class String { ... public String toString() { return this; } ... }
so there is no new string in String's toString method and in trim method new string is created
anil Arikepudi
Greenhorn
Joined: Jun 09, 2005
Posts: 3
posted
0
the first one o/p is true while 2nd one is false why so?
maya rao
Greenhorn
Joined: Jun 10, 2005
Posts: 6
posted
0
Originally posted by anil Arikepudi: the first one o/p is true while 2nd one is false why so?
.equals checks for reference equality. first one is "Equal" cz toString doesnt creats a new string.So both r refering to same object Second one is "Not Equal" cz trim creates a new String.So there r 2 differnet objects
Timmy Marks
Ranch Hand
Joined: Dec 01, 2003
Posts: 226
posted
0
.equals checks for reference equality
Actually, String overrides equals() to return true
if and only if the argument is not null and is a String object that represents the same sequence of characters as this object
but you are comparing above with == which checks the reference.
maya rao
Greenhorn
Joined: Jun 10, 2005
Posts: 6
posted
0
Originally posted by maya rao:
.equals checks for reference equality. first one is "Equal" cz toString doesnt creats a new string.So both r refering to same object Second one is "Not Equal" cz trim creates a new String.So there r 2 differnet objects
Correction to the above statement .equals checks for value equality and == checks for reference equality. Hence the o/p
rajan singh
Greenhorn
Joined: Jun 08, 2005
Posts: 17
posted
0
i have this thought....
whenever we use string literals. String class stores the reference of literal in it's pool which is static member of the string. and if same string literal is used it will not create any new string but get from the pool.. that is why "String".toString() == "String" evalutes to true coz.. we r useing two refernces pointing to same string literal.
where as in " String ".trim() == "String" return flase coz we r using two different string literals " String " and "String" and the value return by trim is new String("String") which is not string literal.
the str3 and str4 must also refer to the same String in the pool. But as you said it returns NOT EQUAL. Why?
rajan singh
Greenhorn
Joined: Jun 08, 2005
Posts: 17
posted
0
hii anil since we r using new operator, which means we are creating two new objects so str3 and str4 pointing to two different objects. and since pool is maintained only for literals not for objects created by new.
Dror Astricher
Ranch Hand
Joined: May 20, 2005
Posts: 31
posted
0
Hi Guys
I thought that in Strings the 2 lines are exectly the same:
String str = "abcd"; String str = new String("abcd");
The String str = "abcd"; is just a shortcut made by Java to make it more comfertable. ???
Can you please fix me if i'm wrong Thanks Guys Have a great weekend
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
posted
0
DROR...
String str1="abcd"; will create string constant in string pool not on heap..
but String str2= new ("abcd"); will put string constant on string pool as well as make an object of String and put in heap.
and also ...
whenever you use any string funtion of String class that modifies the string will result in creating a string object on heap.