• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Question  RSS feed

 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Hope you are doing good..
Have a query...




when s3 is created, it checks in string literal, whether "Rishabh" is exits or not.. and if its exist, s3 will refer same object of s1.
now, In case of s2, why does not happen, even if "Rishabh" is exist......I know s2 is creating and having different object..but, why so....
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rishabh Shah wrote:I know s2 is creating and having different object


Then why are you asking this question? You understand that they are different objects, and the == operator checks for object identity.
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rishabh,

When you are using "new", you explicitly instruct to use a new object, and not existing one.
So from your code,
line 1: create a string "Rishabh", put it into pool and return the reference to s1 variable.
line 2: create a new string "Rishabh", assign it to s2 and if unavaiable, create a object into the pool [already exists so not created]. You are forcing here.
line 3: if "Rishabh" is found in pool, then assign it to s3, else create and assign.

Hope that helps.
 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, Stephan..thanks for reply..
but, I had query that why does s3 not check same thing as it checks for s2 ..whether "Rishabh" exists or not..
anyways, I am clear now..
 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rajdeep Biswas !
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!