The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem in String Class

5 replies on 1 page. Most recent reply: Oct 29, 2002 7:47 AM by Don Hill

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
siddhartha

Posts: 15
Nickname: saddysan
Registered: Jul, 2002

Problem in String Class Posted: Sep 1, 2002 11:09 AM
Reply to this message Reply
Advertisement
Hello friends,
here is a small program

public class A
{
public static void main(String args[])
{
String st1 = " String ".trim();
String st2 = "String";
String st3 = "String";
System.out.println(st1 == st2);
System.out.println(st2 == st3);
}
}

Output is:
false
true

can somebody explains why it is false in first case and true in second

saddy


Ana

Posts: 12
Nickname: anika
Registered: Aug, 2002

Re: Problem in String Class Posted: Sep 2, 2002 1:47 AM
Reply to this message Reply
The == operator is used to compare references to objects, but not values.
st2 and st3 reference the same object, "String", but st1 references a different one.

If we use the equals() method:

System.out.println(st1.equals(st2));
System.out.println(st2.equals(st3 ));

the output would be:

true
true

siddhartha

Posts: 15
Nickname: saddysan
Registered: Jul, 2002

Re: Problem in String Class Posted: Sep 2, 2002 4:05 AM
Reply to this message Reply
Well thanks for your response, but my doubt is still not over, the question is why " String ".trim() creates another object as we are not using new operator, hence it should be a Strring literal as well and not a new object.

if you say that trim() method internally creates new object than just replace " String ".trim() by
"String".trim() and then it will create String literal.
My question is why it is giving different results. Is there any standard rule by which we can determine if we are going to have String literal or Object.
I am getting confused plz help

saddy

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Problem in String Class Posted: Sep 2, 2002 12:10 PM
Reply to this message Reply
In case of a String if it is created at runtime, which is happeneing in case of "String".trim(), then it is always a new String. Otherwise, if String content is known at compile time then it is treated as String literal. In your case st1 is a new String because it is computed at runtime because you are calling trim() method and st2 and st3 are String literals and that is why st2 and st3 are equal as compared by ==. Please follow this link and read the section on String literals and hopefully your doubt will be cleared.
http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101083

Thanks
Kishori

Ramasubbu

Posts: 1
Nickname: ramasubbu
Registered: Oct, 2002

Re: Problem in String Class Posted: Oct 29, 2002 7:17 AM
Reply to this message Reply
Hi,
Whenever you create a String object like this,
String a = "test";
String b = "test";
Internally JVM will create object a by allocating memory and use the same memory for object b. So both references are pointing to the same object. Added to that you are checking is a==b?, true(yes a and b are pointing to the same reference). If you check a.equals(b)? true(yes object a value("test") is equal to object b value)

--Ram

Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Problem in String Class Posted: Oct 29, 2002 7:47 AM
Reply to this message Reply
Another note for you string people,

If you new-up a string as

String a = new String("a");
String b = new String("a");

these string will be pointing to 2 diff strings. Also remember that string are immutable so when ever you apply a method to a string the original string never changes, a new string is created.


HTH

Flat View: This topic has 5 replies on 1 page
Topic: Reading a Flat File using JAVA Previous Topic   Next Topic Topic: Custom class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use