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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Pool" Watch "String Pool" New topic
Author

String Pool

Naveen Sharma
Ranch Hand

Joined: Mar 23, 2001
Posts: 65
String s1 = "Amit";
String s2 = "Amit";
String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(s1.equals(s2));
System.out.println((s1==s2));
System.out.println(s3.equals(s4));
System.out.println((s3==s4));
Answer is
true
true
true
false.
why not All true
thanks
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
Dear Naveen
s3==s4 is false because the string reference s3 & s4 are created using 'new' which always create object in memory separately and that's why it cannot treat it as true.
s3.equals(s4) is true because it only compares the string literal therefore it is true.
On the other hand, string using '=' sign created in a pool when the other same string is created using '=' sign with same string literal it only points out to the same pool where the first object was available and does not creat another copy in the memory. Therefore s1==s2 is always true cuz it points out to the same string pool where s1 is available.
Hope it does not confuse you.
Good luck
Rashid Ali
Apurba Khatua
Greenhorn

Joined: Jul 24, 2001
Posts: 9
1. s1.equals(s2) is true b'cos -> comparing the values of 2 object which is same ie. "amit".
2. s1==s2 is true b'cos -> both have values which is from the same pool of values (where only one copy of "amit" exists).
3. s3.equals(s4) is true b'cos -> see the 1st point.
4. s3==s4 is false b'cos -> the point to 2 different "amit" ( 2 different objects)
Hope this helps.
Apurba.
-------
------------------
Sun Certified Programmer for the Java 2 Platform


A K<br />Sun Certified Programmer for the Java 2 Platform
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
String class overrides equals() & hence it does deep comparison to check whether the contents of the two strings under consideration are equal.
When you create a String using new operator you actually create a new String at Heap with a distinct memory space.
Hence two Strings (S3 & S4) created with new operator reside in different memory locations; i.e their references are different.
== operator checks whether the two objects reside in the same memory space; or in other words do they point to the same address;i.e shallow comparison ; & hence you get it false when u compare two distinct strings (S3 & S4)created with new operator.
String literals as in the case of S1 & S2 are created in String pool and share the same address as long as the Literal is the same. Hence you get both == & equals() returning true.
HTH
any comments......Welcome.
tvs sundaram
[This message has been edited by tvs sundaram (edited July 30, 2001).]
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Shallow and Deep comparisons are the two simple ways to understand this concepts.
== Shallow
equals() Deep
When shallow they look only for references match, thats all
deep does character by character comparison
String does override, the equals() from Object, and the implementation compares it by char.
However some wrappers dont, in that case they just inherit
equals() from Object which is nuthing but shallow (==)
So deep comparison are implementation specific, not a general
defacto

HTH

Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Hi Ragu,
You said some wrapper classes doesn't override the equals method, but I think all the wrapper classes overrides equals() fromm object. Any comments from anyone.
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
I guess i mistyped my explanation
Thankx for pointing me that Farooq
I guess we both had the same discussion in one of the thread
previously

Wrappers do override equals() method. If custom made classes
dont override equals(), then they end up getting the shallow
equals() method from Object
Reda Mokrane
Ranch Hand

Joined: Jul 25, 2001
Posts: 237
HI tvs,
are my string (s1 and s2 )created in String pool?
I think yes.
here is the code

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Pool
 
Similar Threads
Strings
String problem
String literal - strange problem
doubt about Strings from Whizlabs
Two ways to construct a String/pool of strings problem