• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubts in Strings

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I read it. Now what?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the first one o/p is true while 2nd one is false why so?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic