HI, I have the follwoing code: String s1="java"; String s2="java"; String s3=new String("java"); System.out.println(s1==s2); System.out.println(s1==s3); This prints 'true' and 'false' respectively. Any explanation for this?? Thanks -Ravi
s1 and s2 references point to the same object on the String pool, but s3 is created using new operator and therefore is not the same as "java" String literal. It also occupies a different location in the memory. == compares references to the objects in the same class hierarchies, whereas equals() evaluates the equality of the objects based on how equals() method is overridden. So, applying equals() will return true for any combination of s1, s2, and s3. Hope this helps.
== operator compares object references. when the string "java" is assigned to s2, s2 is being pointed to the string "java" which is already on the heap. so, obviously s1 and s2 points to the same objects. Thus, (s1==s2) returns true. Now when s3 = new String("java") gets executed, a new string gets created on the heap as you are using the new keyword to create the string. Thus, (s1==s3) returns false. This is my first reply in Forums .Correct me if am wrong.
Originally posted by K. Ravisekhar Naidu: HI, I have the follwoing code: String s1="java"; String s2="java"; String s3=new String("java"); System.out.println(s1==s2); System.out.println(s1==s3); This prints 'true' and 'false' respectively. Any explanation for this?? Thanks -Ravi
My answer: Every string literal is an instance of class String, and only one is created if they are the same. So s1 == s2 is true, because they are both refering to the same instance of class String. The line:
is actually calling class String's construct String(String s), so a new instance will be created. It is like: String s = "java"; String s3 = new String(s);
Joined: Sep 23, 2003
Hi everyone ! thanks for all your nice explanations. Special thanks to Mr.Vicken Karaoghlanian for explaining 'compile time' and 'run time' literal. Yours, Ravi. [ September 24, 2003: Message edited by: K. Ravisekhar Naidu ]
hi, here is how to look at it 1) s1 --> "java" <-- s2 2) s3 --> "java" 1) is a compile time literal, created at compile time. 2) is a runtime literal, created at runtime.
I thought both are created at runtime. Only difference is that s1 and s2 are pointing to same string in string constant pool while s3 is pointed to a different string on the heap. Anyone, correct me if I am wrong. Thanks Barkat [ September 25, 2003: Message edited by: Barkat Mardhani ]
Joined: Jul 21, 2003
Posted by Barkat:
I thought both are created at runtime
objects that are created using the keyword new are runtime objects (They are always created at runtime)
Creating New Strings Earlier we promised to talk more about the subtle differences between the various methods of creating a String. Let�s look at a couple of examples of how a String might be created, and let�s further assume that no other String objects exist in the pool: 1 � String s = "abc"; // creates one String object and one reference // variable In this simple case, �abc� will go in the pool and s will refer to it. 2 � String s = new String("abc"); // creates two objects, and one // reference variable In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal �abc� will be placed in the pool.
The way I read it is that in both cases string objects are created at runtime. Difference is that in first case, it is created in a special area of memory called string constant pool. In second case (with new operator), it is created in normal (nonpool) memory. Thanks Barkat
Joined: Jul 21, 2003
posted by Barkat
The way I read it is that in both cases string objects are created at runtime
i didn't see the part where it says that both objects are created at runtime!!! this topic was discussed here a couple of months ago, i can't find the link for it but as far as i remember, they came up with the following: 1) String constant pool: memory allocation for compile time literals. 2) Non-pool area: memory allocation for runtime literals. the reason they came up with 'constant pool' ides is for memory optimization, and from my personal point of view i don't think that creating all the strings at runtime will optimize anything, in fact it will be inefficient. at least that what i think. if you disagree please explain. [ September 25, 2003: Message edited by: Vicken Karaoghlanian ]
Joined: Aug 05, 2002
Can any guru help us with this? I think that objects in string constant pool are created at runtime just like the string objects that are created at runtime with new opearator. Vicken's understanding is that objects in string constant pool are created at compile time. I guess the key is determine time of creation
The compiler puts the string constants in the class file.
constant_pool The constant_pool is a table of structures (�4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures.
There is something called as CONSTANT_String_info and I think it is String constant. I believe that when the class gets loaded the string constant from the class file are created in the String pool. ]
Loading of a class or interface that contains a String literal may create a new String object (�2.4.8) to represent that literal. This may not occur if the a String object has already been created to represent a previous occurrence of that literal, or if the String.intern method has been invoked on a String object representing the same string as the literal.
Some one correct me, if I am wrong This is my 2000th post in Javaranch [ September 25, 2003: Message edited by: Pradeep Bhat ]
Welcome to the Ranch Suresh. _________________________________________________________________________ I agree with Pradeep and Barkat all object are created at runtime. The string literals and compile constant expressions (JLS 15.28) that yield a string (*) spawn a CONSTANT_Utf8_info entry in the constant pool of the class declaring them. Such entry is holding the content of the string in a modified UTF_8 format. The compiler makes any subsequent string literal or compile constant expresion holding exactly the same string content to point to such entry. These entries are not of course objects. CONSTANT_Utf8_info holds the content for string literals, full qualifed names for classes and interfaces, simple names of members, and descriptors for members and arrays. At runtime, the first time a CONSTANT_Utf8_info entry, that holds the content of a string literal or string constant compile expression, is used, the "interpreter" looks for an entry in the string pool with the same content as the entry. If it finds it does not create a new String object, it just places the reference to the String object in the corresponding CONSTANT_String_info entry. In this way, all the "subsequent string literals with the same content" are pointing to the same String object. If there is not a match in the search of the string pool the "interpreter" creates one, adds its reference to the pool and places the reference to the new object into the corresponding constant pool entry.
(*) compile constant expression: final String s = "from hell"; String q = "Hello" + s;