Results 1 to 10 of 10
- 04-22-2010, 07:24 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
string comparison with "=" and ".equal"
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
//String s1 = new String("hello");
//String s2 = new String("hello");
if(s1 == s2) System.out.println("true");
}
}
and it printed true when i run the program. I thought s1 and s2 pointed at different memory address so it shouldn't have printed anything.
on my 2nd try, s1 and s2 are both declared as new string(after un-commenting them, and comment out the previous declaration), then it didn't print true as i expected.
how come it prints true on the first try?
- 04-22-2010, 07:34 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Both the compiler and the JVM itself play a few tricks; String literals (the stuff between double quotes) are collected by the compiler and stored once in a .class file. The JVM loads class files and stores those literals in a pool of Strings, all unique. So in your first example there will be only one String object "hello" (stored in that String pool) and both s1 and s2 point to it.
Both references are equals so s1 == s2 is true.
In your second example there's still that single "hello" String in that pool but two different Strings have copied those bytes to their own internal buffers making two different Strings so s1 == s2 will be false.
kind regards,
Jos
- 04-22-2010, 07:45 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
- 04-22-2010, 07:55 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 04-22-2010, 08:17 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
I did actually. it print out s2 = world and s1 stays as hello so s1 didn't get modified. so i proceed to get hashcode value
String a = new String("hello");
String b = a;
System.out.println(System.identityHashCode(b)+","+ System.identityHashCode(a));
b = "world";
System.out.println(System.identityHashCode(b)+","+ System.identityHashCode(a));
System.out.println(b + "," + a);
the hash code for a and b were the same after i do String b = a
here's what i get: 6718604,6718604
after assign "world" to b, the hashcode for b changed, while a stayed the same.
14577460,6718604
the last print line gave me different values
world, hello
b and a had the same hashcode at first,which meant that a and b pointed to the same object right? so when i gave b a new value. how come a didnt get that ?Last edited by guavajuice; 04-22-2010 at 08:21 AM.
- 04-22-2010, 08:23 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 04-22-2010, 08:58 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
based on what happened in the string test, b should be 52, while a stays as 42.
if i have the method in
public static void add(int x) {
x+=10;
}
int a= 42;
int b= a; // both a and b are the same
class.add(b) // modify b value
Then when i print a and b, i get 52 for both. it behaves as i thought, a and b point to the same address, and when value of b changes from 42 to 52, a also has a new value of 52.
When i simply do b = 52, I interpreted it as "modifying the value at address that a and b point to. However, it's actually making a new object (indicated by a new hashcode). Am i understanding this right?
- 04-22-2010, 09:35 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
No.
You get 42 for each.
They aren't pointing at the same memory, since they're primitives.
- 04-22-2010, 08:49 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 5
- Rep Power
- 0
ah. you are right.
if a and b were objects
obj b = a;
b.modify()
then a and b values will both get modified right?
- 04-22-2010, 09:01 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks