• Post Reply Bookmark Topic Watch Topic
  • New Topic

confuse of ==  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,all:

why s1==s2 return true , the s1's value should not equal s2's value.

thanks a lot! waiting on line!
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K & B book states[java lang chapter]:-

For reference variables, == means that both reference variables are
referring to the same object.

I think this shall suffice.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wolfoxer,

Welcome to JavaRanch. We ask that you please change your publicly displayed name to conform to our unique naming convention. You can click here to update that information. Thank you.

With regards to your question, the reason you're getting a "true" response is due to an optimization in Java based on immutability of String objects. The good news is that this is not on the SCJP exam.

However, if you're really interested in this stuff and want to know what I just said, check out this article.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In practice, or real-live working, you should always use equals() or equalsIgnoreCase(). That will really tell you if 2 strings are identical.

The == simply implies that the variables are refering to the same "abc" in the String pool.

So if I do this:



I'll get false, since they're 2 different Strings in the pool.

HTH.
 
lowercase baba
Bartender
Posts: 12579
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you should always use equals() or equalsIgnoreCase().



You should never make absolute statements like this...

There may be a time when you need to know if two strings refer to the same object. Granted, it is probably rare that you'd need to do this, but it may pop up now and then.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:

You should never make absolute statements like this...



Is this not the most ironic statement you've ever heard?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wolfoxer java,

Please take a look at the naming convention again. We ask that you use both a first and last name and also that your name not be obviously fictitious. It doesn't even have to be your real name (many folks use their real names, but that's up to you). Feel free to make up a name, if you want, as long as it "looks" like a real name. Having people use real names helps contribute to the professionalism of JavaRanch.

Thanks,
Corey
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

Originally posted by Cheng Wei Lee:

I'll get false, since they're 2 different Strings in the pool.


the Strings in the pool are always unique. The String(String) copy constructor creates a copy on the "normal" heap.

Conan
 
fred rosenberger
lowercase baba
Bartender
Posts: 12579
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is this not the most ironic statement you've ever heard?



that was my point...

well, one of them, anyway.


[ October 22, 2004: Message edited by: fred rosenberger ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey, can you suggest things that can, and cannot come in SCJP. I was guessing the above topic would most probably come in SCJP. Now i m a little bewildered.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!