String value false

G

gk

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

A. Bolmarcich

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 ?

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.
 
T

Thomas Hawtin

gk said:
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
 
A

attitudenine

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
 
G

gk

attitudenine said:
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


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);

cheers
attitudenine

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

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 ?
 
C

Chris Uppal

gk said:
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
 
P

Patricia Shanahan

gk said:
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 ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top