• 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

String Class

 
Greenhorn
Posts: 18
  • 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");

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

Is 1 the correct answer ?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2
Don't you have a compiler?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Output will be not equal
bcoz when u use any method of string class , it's creates new string object and hence trim()will creat new string and hence results false condition
rahul
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Pawar:
[Output will be not equal
bcoz when u use any method of string class , it's creates new string object and hence trim()will creat new string and hence results false condition
rahul


Hi
But if you do
if("String".replace('t','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

it returns Equal. I am a bit confused: if you use a String method and a new string is created, why does it sometimes return true and sometimes false, in the case of trim();
Or am I missing something really basic. Help!!
Regards,
Sabrina
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sabrina,
String functions are smart! If you tell them to perform something that will do nothing then they will refuse to do it. For example:
Your own example:
replace('t','t'); // Doesn't do anything!
Another
"string".trim(); // Doesn't do anything!
substring(0); // Doesn't do anything!
In all the above cases the methods are smart enough to just return the given string instead of creating a new string. Give the programmers some credit for efficiency!
Regards,
Manfred.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact the optimizer may even toss the instruction from the compiled class.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code :

the method trim, creates a new object (called "String" ).

But why isn't this new object the same that the one created before....
I've read ( I dont remenber exactly where ), that there's something like a string objects pool to make things faster.
Im a bit confused, when does the strings objects are taken from that pool and when do not
 
reply
    Bookmark Topic Watch Topic
  • New Topic