Java Business RIA redefined!
The moose likes Java in General and the fly likes Difference between String s = The Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Difference between String s = "Marcus"; vs String s2 = new String("Marcus"); " Watch "Difference between String s = "Marcus"; vs String s2 = new String("Marcus"); " New topic
Author

Difference between String s = "Marcus"; vs String s2 = new String("Marcus");

amolpalekar kadolkar
Ranch Hand

Joined: Oct 19, 2006
Posts: 176
Hi please tell me difference between
String s = "Marcus";
String s2 = new String("Marcus");
What happens in heap?
vitthal wable
Greenhorn

Joined: Sep 09, 2006
Posts: 16
Hi Amol

In this case a seprate new object gets create in heap having ref as s2
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 959
The only difference is that two object are created.




But in this case, they are different objects




If you would like to use the same object, then use the itern() method of String.



3.10.5 See String Literals in the JLS for further information.
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
1- String objects pool issue

String object is immutable; thats mean when you say

String s = "Mohammed";
s += "El-Adawi";

on runtime this means that a new String "Mohammed El-adawi" is created
and assigned to reference s
and String "Mohammed" now is eligable for gc

why sun have to do that ?, because String literals
(any thing between " " is reccognized by compiler as String literal)
are put by compiler in String pool, then if compilar found the same literal again for example

String s = "abc";
String m = "abc"; // compiler now found the same String literal

compiler will have one copy of "abc" and will assign both s,m to the same String literal, (at run time one object constructed and asigned to s,m)

if m change the String, s will point to the wrong thing, thats why String is immutable.

2- answer to your question

String s = "Marcus"; // One String object is built and assigned to s.

String s2 = new String("Marcus");
/* now compiler found "Marcus" in the pool
so it will not create a new object
new operator creates new object contains
Marcus at runtime
*/


String s3 = new String("Mohammed Not Marcus");
// compiler adds "Mohammed Not Marcus" to String pool but with no
// reference assignment, and String object created and assigned to S3 // at runtime

3- Another thing

String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); // true

but

String s1 = "abc";
String s2 = new String ("abc");
System.out.println(s1 == s2); // false

I hope that is helpfull

SCJP 5
vineela kom
Greenhorn

Joined: Aug 04, 2006
Posts: 7
String s = "Marcus";
It is a string literal.
String s2 = new String("Marcus");
A new instance is created as you have created the string with the new operator.

But again if you have created a new string like
String s1 = "Marcus"

First it searches if the string "Marcus" is there in the string pool,(in this case,as it is already there),now it just reassigns the same to s1.
David O'Meara
Sheriff

Joined: Mar 06, 2001
Posts: 12332
"sriya",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted, often without warning

This is your third and final warning.

thanks,
Dave


Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems. Jamie Zawinski
[JavaRanch FAQ] [Book Promotions] [DbTamer]
Rajeev Kumarjamshedpur
Greenhorn

Joined: Apr 27, 2009
Posts: 5
class StringTest{
public static void main(String args[]){
String s1="rajeev";
System.out.println(s1);
System.out.println(s1.hashCode());
String name=new String("rajeev");
System.out.println(name);
System.out.println(name.hashCode());

}
}

the out put is :
rajeev
-938404389
rajeev
-938404389

So according to our disscussion it is wrong please let me know the exact meaning whats going here
thanks in advance

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 17108
Vineela Kom has given a correct explanation.The hashCode method is overridden in String, so it returns the same value from two Strings which would return true from their equals() method. What you want to do is something like thisStrings not created with the new operator, which are compile-time constants: all the same object. Strings created with new operator: different object. String entered at runtime cannot be predicted by the compiler or JVM, so you can see whether they are the same object.
campbell@computer:~/java$ java StringEqualsDemo Campbell
s = Campbell, hash = fb83cb24, s1 = Campbell, . . .
And work the rest out for yourselves.>
Rajeev Kumarjamshedpur
Greenhorn

Joined: Apr 27, 2009
Posts: 5
Dear Campbell Ritchie

My confusion is according to the discussion when we create
String s1="rajeev";
the jvm ist of all checks whether the same object is already available in the string constant pool .if it is available then it create another reference to it if the same object is not there .then it create anothere object with the constant "rajeev" and store in to string constant pool
System.out.println(s1);
System.out.println(s1.hashCode());
String name=new String("rajeev");

but here the the new operator is used to create the string object . In this case JVM always create a new object without looking in string constant pool
so i am confusion is here why i have the same memory address if jvm create the new object without looking in string constant pool if possible please guide me


System.out.println(name);
System.out.println(name.hashCode());


Thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 17108
The String hash code has nothing to do with the memory address.
 
jQuery in Action, 2nd edition
 
subject: Difference between String s = "Marcus"; vs String s2 = new String("Marcus");
 
The most intelligent Java IDE