Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

String value false

Thread view: 
gk - 02 Nov 2006 20:05 GMT
public class Test {

    public static void main(String[] args) {
        String s1 = new String("amit");
        System.out.println(s1.replace('m','r'));
        System.out.println(s1);
        String s3 = "arit";
        String s4 = "arit";
        String s2 = s1.replace('m','r');
        System.out.println(s2 == s3);// why false ?
        System.out.println(s3 == s4);

    }

}

java support String pool...... s2 is created with the value "arit" and
so it should point to the same pool address.
whats happinh here ?
A. Bolmarcich - 02 Nov 2006 20:31 GMT
> public class Test {
>
[quoted text clipped - 15 lines]
> so it should point to the same pool address.
> whats happinh here ?

s2 and s3 are reference to two different String objects.  Both objects
have the same value, but they are two different objects.  Perhaps,
you meant to write

        System.out.println(s2.intern() == s3);

The intern() method returns a reference to the String object in the pool
whose value is the same as the String on which intern() is invoked.  In
this case, s2.inter() returns a reference to the same object that s3 refers
to because string literals, such as "arit", are automatically interned.
Thomas Hawtin - 02 Nov 2006 22:03 GMT
>         String s2 = s1.replace('m','r');
>         System.out.println(s2 == s3);// why false ?

> java support String pool...... s2 is created with the value "arit" and
> so it should point to the same pool address.

Looking up a String in the internal interned table is a relatively
expensive operation. It's very much faster just to return another object.

Tom Hawtin
Lex - 02 Nov 2006 22:57 GMT
Are you just comparing the String values?  Why aren't you just using
.equals?
attitudenine - 03 Nov 2006 03:32 GMT
String s1 = new String("amit"); //Create NEW String object for "amit"
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3 = "arit"; // "arit" not found in String pool - hence create
new obj
String s4 = "arit"; // same obj as the one referenced by s3
String s2 = s1.replace('m','r'); // NEW String object
System.out.println(s2 == s3);// Hence s2 and s3 are 2 different objects
System.out.println(s3 == s4);

cheers
attitudenine

----------------------------------------------------------------------
"Built exclusively using Attitude Version9.0"
http://orabase.blogspot.com
http://attitudenine.wordpress.com

> public class Test {
>
[quoted text clipped - 15 lines]
> so it should point to the same pool address.
> whats happinh here ?
gk - 03 Nov 2006 10:34 GMT
> String s1 = new String("amit"); //Create NEW String object for "amit"
> System.out.println(s1.replace('m','r'));
[quoted text clipped - 3 lines]
> String s4 = "arit"; // same obj as the one referenced by s3
> String s2 = s1.replace('m','r'); // NEW String object

ok.   but this new string object is also pointing to the same pool
(because the pool HAS a value "arit" in it )....is not it ?
and thats why ,  s2,==s3 should give  true

what do you say ?

> System.out.println(s2 == s3);// Hence s2 and s3 are 2 different objects
> System.out.println(s3 == s4);
[quoted text clipped - 26 lines]
> > so it should point to the same pool address.
> > whats happinh here ?
Chris Uppal - 03 Nov 2006 12:33 GMT
> ok.   but this new string object is also pointing to the same pool
> (because the pool HAS a value "arit" in it )....is not it ?
> and thats why ,  s2,==s3 should give  true

When new Strings are created, Java does /NOT/ check to see if "the same" String
is already present in the pool of interned Strings.

As Thomas said earlier, that would be too expensive to be worthwhile.  (And
probably would cause some semantic problems too).

   -- chris
Patricia Shanahan - 03 Nov 2006 15:14 GMT
> public class Test {
>
[quoted text clipped - 15 lines]
> so it should point to the same pool address.
> whats happinh here ?

Look a bit more closely at the String pool rules, in, for example, the
API documentation for String's intern() method. Creating a String with
the same content as an intern String does not, in general, result in a
reference to the intern String. There are two ways of getting a
reference to a String in the intern pool:

1. The String reference is the result of evaluating a constant
expression of type String.

2. The String reference is the result of calling a String object's
intern() method.

s3 and s4 both refer to constant expressions of type String, so they
point to the "arit" in the pool.

Each of s1 and s2 contains neither the result of evaluating a String
constant expression, nor the result of an intern call. Neither should
point to the pool.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.