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

Question abt Strings

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Im going to take the exam next week and i was very confident to do well but after i took abhilash's Quiz i feel the is more to it. Here is a question on String which serprised me. Can anyone explain why its so.
The answer is "Equal" and "Not Equal".
public class AQuestion {
public static void main(String args[]) {
f("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");
}
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first If statement, "String" constructs a new String object. The toString() method returns the SAME OBJECT if the type of object is String. The Second appearance of "String" does not construct a new String object, it just refers to the previously created String object.
Hence "String".toString() == "String" is true.
In the second if statement, the " String ".trim() causes new String object to be constructed with " String " as its value. The trim() method returns a NEW String object. The second appearance of "String" just refers to the String object created in the first if statement. Hence the two variables do not refer to the same object.
Hence " String ".trim() == "String" is false.
Vikram
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone give me the URL to Abhilaash Quiz
Thx
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikram,
As u said...the trim() causes new String object to be constructed.... see the code below.... The output is Equal.....!!!
Isn't 2 new strings created here
if("String".trim() == "String".trim()) ?
class Test {
public static void main(String args[]) {
if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Thx
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikram, just one addition. Even without the first 'if' statement it would be the same result. The matter is when JVM sees a code like "String" it looks through a heap to find out if such a string is already there. If so, it references the same String (without creating new one). If not, it creates a new Object. But the trim() function explicitly creates a new String object (except the case when there is nothing to be trimmed). So, it's just needed to know when String's functions (like toUpperCase(), concat(), toLowerCase(), trim() ..) return the same reference and when they create new one. As usual, if no changes have to be done, the String's function returns the same reference, otherwise it creates a new object.
So, "string".toLowerCase() == "string" is TRUE in JDK1.2 where that function is a little bit optimized but FALSE in JDK1.1 where it always creates a new object.
 
Sanjeet Karamchandani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I agree with yanish. he is quite right. The thing is if a new object is to be returned then the objects r not equal otherwise they r.
Now i now it.
Thanks for ur help
Sanjeet
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just a little more on the replies already given :
When a string literal is used ("string"), Java creates a String object and places it in a pool of strings. If the same literal is used again, the String object already excisting in the pool is used and no new object is created.
However, if a String object is explicitly created using
String s = new String("string");
even though the sequence of characters is the same as the previous literal, new String object is actually created and placed on the heap.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic