• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please Answer soon with explanation  RSS feed

 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello there!
Please Explain

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

Answers
1.the code will compile an print "Equal".
2.the code will compile an print "Not Equal".
3.the code will cause a compiler error
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the source of String, if there is white space in the String that you call trim on, it will return the output of the substring method. The substring method creates a new String unless the parameters to it are 0 and the length of the String.
 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey keith

thanks for reply but i do not get exactly wat u want to say
so please make it more clear


Sabber Bhatia
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you entered the code into a program and tested it?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what output did you get when you ran it, and why do you think that is?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also read the API for the String.trim() method.
[ June 27, 2006: Message edited by: Barry Gaunt ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Literal Pool is a very good article for String topic.
 
Srikanth Kowtha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello there!
Please Explain

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

Answers
1. the code will compile an print "Equal".
2. the code will compile an print "Not Equal".
3. the code will cause a compiler error


The answer is 2.
When you compare two string objects with a "==", thats means you are comparing the object references. and definitely they are not equal.
the answer would have been 1, if the condition was
if(" String ".trim().equals("String"))

 
tapan saha
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More over:


Case 1:

if("String".trim() == "String") // with-out space
System.out.println("Equal");
else
System.out.println("Not Equal");


For this ,it will refer same object for that output will be "Equal".

Case 2:

if("String ".trim() == "String") // with space
System.out.println("Equal");
else
System.out.println("Not Equal");


For this ,it will create new object for that output will be "Not Equal".

Case 3: (added by Srikanth Kowtha )

if(" String ".trim().equals("String")) // string compare
System.out.println("Equal");
else
System.out.println("Not Equal");


For this, string compare for that output will be "Equal".
 
Pinkal Patel
Ranch Hand
Posts: 57
  • 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");


If you used


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


Than it will return Option 1)

will compile and o/p Equal
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"String".trim() will return the origianl reference. The "String" String object is in String literal pool.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!