Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts in Strings  RSS feed

 
anil Arikepudi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I read it. Now what?
 
Pete Starsky
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the first one o/p is true while 2nd one is false why so?
 
maya rao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!