Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String constant pool??

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Consider the code below from Dan Chisholm,



I was thinking that c = a+b; and d = a + b; will refer to the same onjects in the memory?

How come Line 1 prints false?? Anyone please?
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the declaration is modified like this,
final String a = "A", b = "B", c = a+b, d = a+b;
(c == d) will be true.

Hope you will be able to conclude.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinayagar,

c == d will also return false.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:

c == d will also return false.



I don't understand why you got false on this condition(c==d)?



this programe prints "true" same in all 3 lines..
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I executed the code and it gave me false, false, true.
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This program prints true and true.
When a or b or both changed to non-final, both are false.
Hope this helps.
 
Mark Uppeteer
Ranch Hand
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true
true
true

regards,
Mark
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi..

yes you are write dear. the output is

false
false
true

As i know, String objects are immutable. yes it is 100% true.
then why concat operation performed on String.

in //line1

left side of == concat two string object(a and b)results into a 3rd new object lets say x,and right side of == also concat two string object(a and b)results into a fourth new object lets say y.
.........
then dear both x and y two different object created in memory....hence == operator returns false...

line 2 evaluated similer way...

hope this helps...Anu
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anuragk kushwaha:

yes you are write dear. the output is

false
false
true



Originally posted by Jothi Shankar Kumar Sankararaj:
I executed the code and it gave me false, false, true.


hi Shankar & anuragk,

did you guys try my one.. which i wrote above.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif,

I tried your code and it gave me true, true, true.

Can you tell me the logic why if one is a final String then it prints true?? Confused??
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saif,

yes, final makes a diffrenve, would you please discribe it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic