This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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

explain this string method..

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
why this is printing not equal, because when string is trimmed
it should be equal to String..,
can someone explain
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bhasker Reddy:
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
why this is printing not equal, because when string is trimmed
it should be equal to String..,
can someone explain

I think you need to read the chapter on "Strings" once more.
This is the most basic thing, here you are trying to compare the addresses of the two strings with the operator ==. I you want to compare the strings as such(the contents of the strings), you need to use the method - equals() as:
" String ".trim().equals("String")
- you will get the correct result.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following code will print Equal
if ("String"=="String")System.out.println("Equal");

because there is only one string object is created.
This is equivalent to saying,

String a="String";
String b="String";
if (a==b) System.out.println("Equal"); // will print Equal

But " String ".trim()=="String" is not equal because
" String ".trim() creates a new String object.
This is equivalent to

String a="String";
String b=new String("String");
if (a==b) System.out.println("Equal");
else System.out.println("Not Equal"); // will print not equal

 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about
String a=new String("String");;
String b=new String("String");
if (a==b)
System.out.println("Equal");
else
System.out.println("Not Equal");
It stills prints Not Equal. I thought this time it should point to the same memory address.
Your inputs will be highly appreciated.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String a=new String("String");
String b=new String("String");
The above code creates 2 string objects.
The following mock exam has a lot of questions on string equality.
http://members.theglobe.com/apoddar/questions.html
 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srini
I need to do a lot of reading and practice cos I'm giving exam on Aug 18.
THanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic