aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubts in Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Doubts in Strings" Watch "Doubts in Strings" New topic
Author

Doubts in Strings

anil Arikepudi
Greenhorn

Joined: Jun 09, 2005
Posts: 3
Read this piece of code carefully

if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

------------------------------
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Ok, I read it. Now what?
Pete Starsky
Greenhorn

Joined: Jun 09, 2005
Posts: 1
Quite simple

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
the first one o/p is true while 2nd one is false why so?
maya rao
Greenhorn

Joined: Jun 10, 2005
Posts: 6
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
.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
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
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.

we can counter check by this example

String str1 = "string";
String str2 ="string";
String str3 = new String("string");
String str4 = new String("string");
str1==str2 => true;
str1==str3 => false;
str3==str4 => false;
anil Arikepudi
Greenhorn

Joined: Jun 09, 2005
Posts: 3
HI

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
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
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
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.

like Str1=str1.toUpperCase();

will make an object on heap...


Thanks and Regards,<br />Amit Taneja
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts in Strings
 
Similar Threads
Please Answer soon with explanation
== doubt
string
String Question
Trim()