wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 ""==" in String Class and Wrapper Class" Watch ""==" in String Class and Wrapper Class" New topic
Author

"==" in String Class and Wrapper Class

Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
String s1="Priyam";

String s3="yam";
String s2="Pri" + s3;
System.out.println("s1==s2:: " + (s1==s2)); //Output false

but such is not the Output in Wrapper Classes

Integer int3 = 5;
Integer int5 = 2;
Integer int4 = 3 + int5;
System.out.println("int3==int4:: " + (int3==int4)); //Output true

why is a different O/P in Wrapper Classes even though same thing is done in both cases!!!


"History would be kind to me, for I intend to write it."
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Hi Priyam,

Strings which are immutable uses String constant pools and whenever the same string constants appers in the same program (should say JVM),allows the previously existed string to be reused.
So in this case when we have Priyamalready in the string pool,will be reused.so here in the code

s1 and s2 will point to same string Priyam which has been created and exists in the pool at the time when you declared s1=Priyam.

In the second case there is no pooling concepts.when you does a operation on wrapper objects first its value should be extracted for the further arithmetic operation ,then the real job i.e. airthemetic calcuation then again boxing into the wrapper type.



int5 will be unwrapped then will be added with 3 then again wrapped to form a new wrapper object and finaly int4 will get a reference to this newly created object.


~Sanjeev Singh<br />SCJP 1.5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers!

Sanjeev Kumar Singh posted November 04, 2006 02:42 AM



s1 and s2 will point to same string Priyam which has been created and exists in the pool at the time when you declared s1=Priyam.



That's ok, but:


prints "false".

And now?

Yours,
Bu.


all events occur in real time
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Hmmmmmm ...Thanks for pointing..

No answer
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
String s1="Priyam";
String s3="yam";
String s2="Pri" + s3;

s1 and s2 will point to same string Priyam which has been created and exists in the pool at the time when you declared s1=Priyam.


acutally == checks that both refrences point to same object.
here s1, s2 points to same object. so s1 == s2 should return true only.
but it return false.

please explain this.
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
so none of the ranchers are having any reply to my Question???
Help me out guys!!!
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
first do know this-
Integer i1=1;
Integer i2=1;

here i1==i2 returns "true" and i1!=i2 returns "false"
and
but
Integer i1=1000;
Integer i2=1000;
here i1==i2 returns "false" and i1!=i2 returns "true"

here is the basic rule Two instances of the followin wrapper objects will always be == (means return true) when the "primitives "values" " are same

1. Byte
2. Boolean
3. Character from \u0000 to \u007f
4. Short and Integer from -128 to 127

NOW for Strings
== return true when two strings been compared are either literals or compiled time constants but not created usin new keyword and one more thing explained later;

for eg : "Atul"=="Atul" returns true
or
String s="atul";
"atul"==s return true
but
String s="atul";
String h="ul";
String i="at"+h; returns false
or
String s="atul"; // (A)
String h="";
String i="atul"+h; returns "false" //"one more thing"
but
String s="atul";
String h="ul";
String i="atul"+""; returns true
and
String s="atul";
final String h="ul";
String i="atul"+h; returns "true" //(compare it with (A))
one more thing that i promised to tell later ..u can infer through (A)

now coming to question

String s1="Priyam";

String s3="yam";
String s2="Pri" + s3;
System.out.println("s1==s2:: " + (s1==s2)); //Output false

String is returning false as explained in (A)

and for Wrapper Integer
Integer int3 = 5;
Integer int5 = 2;
Integer int4 = 3 + int5;
System.out.println("int3==int4:: " + (int3==int4)); //Output true

you can infer from the explanation given on the top

There is nothing to compare in functionality of == in String and Integer

String works for String "literals" and Wrapper for "primitive "values""

hope this much helped
Regards
-Atul
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
some typos in above answer so read this version--

first do know this-
Integer i1=1;
Integer i2=1;

here i1==i2 returns "true" and i1!=i2 returns "false"
but
Integer i1=1000;
Integer i2=1000;
here i1==i2 returns "false" and i1!=i2 returns "true"

here is the basic rule Two instances of the followin wrapper objects will always be == (means return true) when the "primitives "values" " are same

1. Byte
2. Boolean
3. Character from \u0000 to \u007f
4. Short and Integer from -128 to 127

NOW for Strings
== return true when two strings been compared are either literals or compiled time constants but not created usin new keyword and one more thing explained later;

for eg : "Atul"=="Atul" returns true
or
String s="atul";
"atul"==s return true
but
String s="atul";
String h="ul";
String i="at"+h; returns false
or
String s="atul"; // (A)
String h="";
String i="atul"+h; returns "false" //"one more thing"
but
String s="atul";
String h="ul";
String i="atul"+""; returns true
and
String s="atul";
final String h="";
String i="atul"+h; returns "true" //(compare it with (A))
one more thing that i promised to tell later ..u can infer through (A)

now coming to question

String s1="Priyam";

String s3="yam";
String s2="Pri" + s3;
System.out.println("s1==s2:: " + (s1==s2)); //Output false

String is returning false as explained in (A)

and for Wrapper Integer
Integer int3 = 5;
Integer int5 = 2;
Integer int4 = 3 + int5;
System.out.println("int3==int4:: " + (int3==int4)); //Output true

you can infer from the explanation given on the top

There is nothing to compare in functionality of == in String and Integer

== in String works for String "literals" and == in Wrapper for "primitive "values""

hope this much helped
Regards
-Atul
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
or eg : "Atul"=="Atul" returns true
or
String s="atul";
"atul"==s return true
but
String s="atul";
String h="ul";
String i="at"+h; s==i returns false
or
String s="atul"; // (A)
String h="";
String i="atul"+h; s==i returns "false" //"one more thing"
but
String s="atul";
String h="ul";
String i="atul"+"";s==i returns true
and
String s="atul";
final String h="";
String i="atul"+h; s==i returns "true" //(compare it with (A))
Ram Kumar
Ranch Hand

Joined: Jun 19, 2006
Posts: 39
package basics1;

public class StringTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "ramkumar" ;
String s2 = "kumar" ;
String s3 = "ram" + s2 ; // Literal and object so creates a new reference to preserve immutability.
String s4 = "ram" + "kumar" ; // Literals only so re-uses from the string constant pool as the resultant value alreayd exists in string constant pool, moreover.
System.out.println((s1==s3) + " " + s1.hashCode() +"\t" +s3.hashCode()) ;
System.out.println((s1==s4) + " " + s1.hashCode() +"\t" +s4.hashCode()) ;
}
}

//PRODUCES..
//false 228414038228414038
//true 228414038228414038

The hashcode for all these objects are same but I wonder why the comparison is giving false and true differently. I know if the object value is same the hashcode may be same as per hashCode implementation of String class. The actual reference is different!.

This says that;

1) When you are using string literals for assignment and also for concat and assignment and compare the values using == you will 'true' if the object values are equal.

2) If you are concatinating strings using literal and object or object and object the resulting string is a new String object with same value and hence will result in 'false' when compared using ==.
[ November 04, 2006: Message edited by: Ramkumar Sridharan ]

-- SCJP 5, OCPJBCD 5, OCPJWSD 5 (in progress)
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
hey Ramkumar i couldn't get that implications from hashcode values...

if .equals results in true then hashcode must be equal
if hashcodes are equal then there is no requirement for equals to be true..
if hascodes are not equal then equal must be false


please explain
Ram Kumar
Ranch Hand

Joined: Jun 19, 2006
Posts: 39
Atul obviously the hashcode method of strings are fine!. What I meant was if hashcode wasn't implemented (for eg.,) we would see different hashcodes (from object) if they are different objects. Since, hashcode is implemented in string they all produce the same value because the value they hold is same as well!. The s1 and s4 is pointing to the same instance resultin in true, while s1 and s3 is different object altogether resulting in false even though they hold same value internally.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Guys,

I very vaguely remember that there is a certain limit on the wrapper classes where it considers two references to be equal. cosider this as below,

Integer a = 1;
Integer b = 1;
Now if you say a == b, it returns true.

Integer a = 10000;
Integer b = 10000;
Now if you say a == b, it returns false.

So the point is to certain limit, the references of wrapper classes will be the same or in other words will point to the same object but when it exceeds...I dont know the exact value....it prints false or in other words it points to different objects in the memory.

Please also remember that for Strings,

String s1 = "Joe";
string s2 = "shan";
String s3 = s1;
s1 == s3 //returns true
String s4 = "Jo" + "e";
s1 == s4 //returns true
String s4 = "Jo";
String s5 = s4 + "e";
System.out.println(s5 == s1); // returns false....can you guys tell??

The reaason is whenever you say String reference + "xyz" (s4 + "e"), a new object is created. Remember this golden rule. A string reference + a string will create a new object in the memory, never matter if that object is already there in the String pool. But on the other hand when you say "Jo" + "e", the String is concatenated and looked for in the pool if the same object exists and if yes, the reference points to that alredy existing object.

Hope you gugs got it. Can anyone tell what is the limit for the wrapper classes values for the refernces return true???

Regards,
Jothi Shankar Kumar. S
[ November 04, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

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!
Atul Shukla
Ranch Hand

Joined: Oct 09, 2006
Posts: 34
hi! jothi shankar
thanks for the nice little tutorial
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Hi all,
Posted by Jothi Shankar Kumar Sankararaj
Hope you gugs got it. Can anyone tell what is the limit for the wrapper classes values for the refernces return true???

This answer lies in the answer posted by Atul Shukla

here is the basic rule Two instances of the followin wrapper objects will always be == (means return true) when the "primitives "values" " are same

1. Byte
2. Boolean
3. Character from \u0000 to \u007f
4. Short and Integer from -128 to 127
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Sanjeev,

Guess that you didnt get what I told.

Try,

Integer a = 1;
Integer b = 1;
a == b returns true why??

but on the other hand,

Integet x = 10000;
Integer y = 10000;
x == y returns false why?? Still both x's and y's primitive are same (10000)

The asnwer that I gave in the above post of mine is partially correct. To some limit or value, the Integer considers references point to the same object and I just want to know that limit. Can anyone please tell me.

Regards,
Jothi Shankar Kumar. S
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Thanks Jothi,
I think is given avobe for int and short that is -128 to 127.And for why? I think it is the API writers who can answers better!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "==" in String Class and Wrapper Class
 
Similar Threads
about string
String problem
about "=="and "equals" problem,I can't understand clearely?
When the JVM create a new Wrapper and when the JVM use a living one? ( K&B 5.0)
== with Strings is giving a different result