Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"==" in String Class and Wrapper Class

 
Priyam Srivastava
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmmmm ...Thanks for pointing..

No answer
 
sentil kumar
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so none of the ranchers are having any reply to my Question???
Help me out guys!!!
 
Atul Shukla
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Atul Shukla
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Atul Shukla
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi! jothi shankar
thanks for the nice little tutorial
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic