• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Please Answer soon with explanation

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you entered the code into a program and tested it?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also read the API for the String.trim() method.
[ June 27, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String Literal Pool is a very good article for String topic.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"))

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"String".trim() will return the origianl reference. The "String" String object is in String literal pool.
 
reply
    Bookmark Topic Watch Topic
  • New Topic