Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Mamta Swain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
System.out.println(("a"+"b")==("a"+"b"));
System.out.println(("a"+"b")==("ab"));
System.out.println(("a".concat("b"))==("a".concat("b")));
The above prints as
true
true
false.
Could anybody explain me the above ?
How can we determine in which operations the String produces a new one ?
Thanks

 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mamta Swain:
Hi
System.out.println(("a"+"b")==("a"+"b"));
System.out.println(("a"+"b")==("ab"));
System.out.println(("a".concat("b"))==("a".concat("b")));
The above prints as
true
true
false.


hi Mamta
in String when we have a string 'litral' JVM creates an object and after that for the same litral it just give reference to all ready created object.
Exa:
String aObj = "String"
String bObj = "String"
now as "String" is litral, an String object(one object only) will be created and aObj & bObj both will refer that created object and after that when ever there will be same string litral it will refer to already created litral object.
you can run this prog
<pre>
class StringTest {
public static void main(String args[]){
String aObj = "String";
String bObj = "String";
System.out.println(aObj == bObj);
System.out.println(aObj.equals(bObj));
System.out.println(aObj == "String");
System.out.println(bObj == "String");
}
}
</pre>


here you will get true for all. Reason "String" object is creted once and being referenced each time.
Now let us come to your problem:
<Pre>
System.out.println(("a"+"b")==("a"+"b"));
System.out.println(("a"+"b")==("ab"));
System.out.println(("a".concat("b"))==("a".concat("b")));
</pre>
1) System.out.println(("a"+"b")==("a"+"b"));
when you "a" and "b" it is created and now will be refered by all other string litral. And when you concat them ("a" + "b") an another string litral is created and and afterward where ever jvm found the equal string litral it will refernce the alraedy creted one. henec "a" + "b" you can write thousand time , there won't be thousand String objects.
they all will refer to same alraedy created object. And '==' operator compares the references not the object.
So the o/p is true.
2) System.out.println(("a"+"b")==("ab"));
as "ab" litral has been already created so all the "ab" litral will refer to the same already created object.
so o/p is true.
3)System.out.println(("a".concat("b"))==("a".concat("b")));
String is immutable objects i.e. once they are created they can not be changed.
So when ever you use any method of String class which returns String, it does not(can not) modify the already existing object. It always creates a new object and returns.(Key point is that, these new creted object may contain the same string litral i.e. in my above prog if you add:
<pre>
String cObj = new String("String");
String dObj = new String("String");
System.out.println(cObj == dObj); // will return false
</pre>
)
so when you are using String method concat() it is creating a new object at LHS of '==' operator and another object at RHS of '=='.
As == compares references not the objcet(or contents of object) so it returns false.
That is why it is said that whenever you want to compare to objects you should use equals() method. And please do see the signature of equals() method. There is some thing to note about equals() signature. For your class you might up end with overloading equals() rather then overriding.
All The Best
Hope it helps you
correct me if I am wrong
[This message has been edited by ravish kumar (edited October 18, 2001).]
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First two are comparison of string literals. String literals
are always interned. (See below).
Since the result of "+" are "ab", they all share the
same instance, both lefthand and righthand of ==.
Therefore, == operation is evaluated to "true".
As for the third, the method "cancat" is involved. It returns
a separate instance of string "ab" on each invokation. So,
the evaluation of == is false.

JLS 3.10.5:
String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern.
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mamta,
See the following code.. It will clear to you..

JLS 3.10.5:
String literals-or, more generally, strings that are the values of constant expressions(�15.28)-are "interned" so as to share unique instances, using the method String.intern.
class StringTest{
public static void main(String args[]){
String b="b";
System.out.println(("a"+"b")==("a"+"b"));//1.true..
System.out.println(("a"+"b")==("ab"));//2.true
System.out.println(("a"+"b")==("a"+b));//3.false
System.out.println(("a"+b)==("a"+"b"));//4.false
System.out.println(("a".concat("b"))==("a".concat("b")));//5.false
}
}
//1,//2 are true becase they are constant expressions of string literals and evaluate at compile time, and they are interned(share same instances) so it return true..
//3,//4 are false because they are not constant expressions of string.. where b is evaluate at runtime, so new instance of string created and it returns false..
I hope this help you..
regards,
Suresh
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More Example...


Shyam

[This message has been edited by Shyamsundar Gururaj (edited October 19, 2001).]
 
Mamta Swain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for giving such wonderful explanations.
Mamta
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very well explained...
Originally posted by ravish kumar:

hi Mamta
in String when we have a string 'litral' JVM creates an object and after that for the same litral it just give reference to all ready created object.
Exa:
String aObj = "String"
String bObj = "String"
now as "String" is litral, an String object(one object only) will be created and aObj & bObj both will refer that created object and after that when ever there will be same string litral it will refer to already created litral object.
you can run this prog
<pre>
class StringTest {
public static void main(String args[]){
String aObj = "String";
String bObj = "String";
System.out.println(aObj == bObj);
System.out.println(aObj.equals(bObj));
System.out.println(aObj == "String");
System.out.println(bObj == "String");
}
}
</pre>


here you will get true for all. Reason "String" object is creted once and being referenced each time.
Now let us come to your problem:
<Pre>
System.out.println(("a"+"b")==("a"+"b"));
System.out.println(("a"+"b")==("ab"));
System.out.println(("a".concat("b"))==("a".concat("b")));
</pre>
1) System.out.println(("a"+"b")==("a"+"b"));
when you "a" and "b" it is created and now will be refered by all other string litral. And when you concat them ("a" + "b") an another string litral is created and and afterward where ever jvm found the equal string litral it will refernce the alraedy creted one. henec "a" + "b" you can write thousand time , there won't be thousand String objects.
they all will refer to same alraedy created object. And '==' operator compares the references [b]not
the object.
So the o/p is true.
2) System.out.println(("a"+"b")==("ab"));
as "ab" litral has been already created so all the "ab" litral will refer to the same already created object.
so o/p is true.
3)System.out.println(("a".concat("b"))==("a".concat("b")));
String is immutable objects i.e. once they are created they can not be changed.
So when ever you use any method of String class which returns String, it does not(can not) modify the already existing object. It always creates a new object and returns.(Key point is that, these new creted object may contain the same string litral i.e. in my above prog if you add:
<pre>
String cObj = new String("String");
String dObj = new String("String");
System.out.println(cObj == dObj); // will return false
</pre>
)
so when you are using String method concat() it is creating a new object at LHS of '==' operator and another object at RHS of '=='.
As == compares references not the objcet(or contents of object) so it returns false.
That is why it is said that whenever you want to compare to objects you should use equals() method. And please do see the signature of equals() method. There is some thing to note about equals() signature. For your class you might up end with overloading equals() rather then overriding.
All The Best
Hope it helps you
correct me if I am wrong
[This message has been edited by ravish kumar (edited October 18, 2001).][/B]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic