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 consant pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String consant pool" Watch "String consant pool" New topic
Author

String consant pool

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71


It gives the output
arit
amit
false
true

Why doesn't it give the output
arit
amit
true
true


Because it's giving false does it mean s2 is not refering to the string already in the String constant pool called "arit".
Why does it create a new object when String "arit" already exists.
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
The line String s2=s1.replace('m','r'), this actually creates a new object in the heap.It means that s2 now is pointing on a different object.When you are going to use the == for comparison it will compare both operands via their reference type. It is another story if you are going to use the equal operator(s2.equals(s3) which will turn out true.Both operands
are compared by their value.

arnel
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Another thing, String are immutable. So if you you use any
of its methods it actually creates another String object.

arnel
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
And none of this is on the exam.


SCJP Tipline, etc.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Originally posted by Corey McGlone:
And none of this is on the exam.


Nice to know that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String consant pool
 
Similar Threads
Doubt with String method "replace"
one more on ==
about string
question on Strings
Doubt in Strings