• 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

Strings & their methods

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This program prints Not Equal.
A new string is created (s3), and the new string is identical to a string already in the string pool (s1). Why does the compiler not recognize that the new string (substring of the original string) is identical to the string already existing in the literal string pool?
Marilyn
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,
It is because String s3 = s2.trim() has the same effect as
String s3 = new String("abc") in this case. Specifically, the trim function instantiates a new String object rather than simply returning the address of the String literal "abc".
Hope this helps,
Bob Kerfoot - SCJP
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,
s1==s3 compares the references s1 and s3 and not what is contained in them. Therefore it will always return false.
s1.equals(s3) will return true(I didn't try this but it will work).
Hope this helps and Good Luck
Ambrose
 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ambrose,
This is not always the case. For example if we have
String s1 = "abc";
String s3 = "abc";
then (s1 == s3) evalutes as true because both references point to the same String "abc" in the literal pool.
Bob Kerfoot - SCJP
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob,
You are right in what you say, but
s3=s2.trim();
will instantiate a new object for s3 even though it equals s1 (that is, it does not behave the same as s3="abc"
Please correct me if I m wrong.
Ambrose
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see here http://www.javaranch.com/ubb/Forum24/HTML/001933.html
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. Everything is clear now.
Marilyn
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob, Ambrose Marlyne
Sir !! Do you think this following code will compile ??
I don't think so , "Can't make a static reference to nonstatic variable s2 in class Test."
If I am wrong please correct me.
Thanks
jaydeep

class Test
{
String s1 = "abc" ;
String s2 = " abc " ;
public static void main ( String [] args )
{
String s3 = s2.trim() ;
if(s3 == s1)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jaydeep, you are right. Since String s1 and String s2 are not static, then main cannot access them without creating an instance of Test class an accessing them through the instance. Good point. Make s1 and s2 static and you are fine.
 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jaydeep,
You are correct and I noticed the same. However, I figured that the String concepts were and are the point of focus so I did not mention it. Good catch though.
Bob Kerfoot - SCJP
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob,
This is because the compiler does some basic optimization here. It understands that both the string literals are same and chooses to create only one object in the pool. This in my opinion would then be compiler dependent.
Varsha

Originally posted by Bob Kerfoot:
Ambrose,
This is not always the case. For example if we have
String s1 = "abc";
String s3 = "abc";
then (s1 == s3) evalutes as true because both references point to the same String "abc" in the literal pool.
Bob Kerfoot - SCJP


 
Bob Kerfoot
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Varsha,
This is not compiler dependent behavior. It is a specific String behavior requirement specified in the JLS. Take a look at �3.10.5 for the specifics.
Let me know if you have any other doubts.
Hope this helps,
Bob Kerfoot - SCJP
 
Varsha Dighe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bob for clearing that !
-Varsha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic