File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String constant pool?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String constant pool??" Watch "String constant pool??" New topic
Author

String constant pool??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
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

Joined: Sep 26, 2006
Posts: 9351
    
    2

Vinayagar,

c == d will also return false.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

I executed the code and it gave me false, false, true.
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72


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

Joined: Mar 02, 2004
Posts: 159

true
true
true

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Anuragk kushwaha
Ranch Hand

Joined: Aug 02, 2006
Posts: 51
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

Joined: Dec 06, 2005
Posts: 1321

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

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Aug 02, 2006
Posts: 51
Hi Saif,

yes, final makes a diffrenve, would you please discribe it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String constant pool??