This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General problems..

 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

prints Not equal.

why that so??

it happens also if



 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

use equal method whenever you want to compare two strings. The following will result in equal as an output:

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, == is checking reference identity (do the references refer to exactly the same String object in the heap?) In this case you have different dynamically generated String objects, so the == test will return false.
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String object remains in heap and if any string present there then that string refers to that string
thus


but not for above mentioned cases why??


 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look into the replace method you find this line to create a new String:

And as we know, if the new keyword is used, a new object is created even if this string literal is already there.
And if a new string object is created, your code prints a not equal.

check also this out:

This code also creates two string objects.

Hope I could help.
cheers
Bob
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikky.ag agrawal wrote:String object remains in heap and if any string present there then that string refers to that string


Well you need to understand that there is a pool of strings maintained by the JVM for faster String processing. It would be worth to give this article a reading...
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob wrote..
If you look into the replace method you find this line to create a new String:


Thanks bob it really helped me..
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was just trying to practice and

why the value of s4 didn`t point to s3 in heap??

 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bottom line is that every time you mutate a String object, you get a brand new String object which is not same any other String objects created earlier (even if the content is same). So here s3 is new object and so is s4. That's why == comparison shows as false.
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santhosh Kumar wrote:Bottom line is that every time you mutate a String object, you get a brand new String object which is not same any other String objects created earlier (even if the content is same). So here s3 is new object and so is s4. That's why == comparison shows as false.

thanks santosh i got the idea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic