This question already has answers here:
The return of String.intern() explained
(3 answers)
Closed 3 years ago.
I'm learning Java now, and i read this question:What is Java String interning? - Stack Overflow
but i read other articles which provide some examples i don't understand:
public static void main(String[] args) {
String str2 = new String("str") + new String("01");
str2.intern();
String str1 = "str01";
System.out.println(str2 == str1); //true
String s4 = new String("1");
s4.intern();
String s5 = "1";
System.out.println(s4 == s5); //false
String s = new StringBuilder("aa").append("bb").toString();
String s2 = new StringBuilder("cc").toString();
System.out.println(s.intern() == s); //true
System.out.println(s2.intern() == s2); //false
}
the result in Java 11( it should be same in Java 8) is :
true
false
true
false
i don't know why results are different, i suppose it to be all true.
can anyone explains it?
Let's look at what intern actually does (emphasis mine):
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
When this code is run:
str2.intern();
The string pool only contains "str" and "01". str2 is "str01", which is not in the string pool, so the object that str2 refers too is added to the string pool. Therefore, when the next line is reached, since "str01" is already in the pool, str1 will refer to the same object as str2.
Note that the reason why str2 == str1 is not because intern somehow changes which object str2 refers to. It doesn't do anything to str2. Strings are immutable after all.
Now we can understand the first false. In the line:
String s4 = new String("1");
Because of the string literal "1", "1" is added to the string pool. Note that the object that is added to the string pool is not the same as the object to which s4 refers. Now you call s4.intern(), which does nothing to s4, and returns the object that is in the string pool. But you are ignoring the return value. This is why s4 != s5, and s4.intern() == s5.
The reason for s2.intern() != s2 is much simpler - s2 refers to a different object than "cc" in the string pool! s2.intern() is supposed to return the object in the string pool, so of course it is not the same object!
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 3 years ago.
public class Test {
public static void main(String[] args)
{
String s1 = "HELLO";
String s2 = "HELLO";
System.out.println(s1 == s2); // true
}
}
But when I use :
public class Test {
public static void main(String[] args)
{
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2); // false
}
}
Can anybody please explain the difference here? Thankyou!
In the first example
String s1 = "HELLO";
String s2 = "HELLO";
the values of s1 and s2 are compile-time constants. Thus, the compiler does only generate a single String-object, holding the value "HELLO" and assings it to both s1 and s2. This is a special case of Common Subexpression Elimination, a well-known compiler optimization. Thus s1 == s2 returns true.
In the second example, two different Strings are constructed explicitly through new. Thus, they have to be separate objects per the semantics of new.
I created an Ideone demo a while back that highlights some cases that show this behaviour.
You can enforce that the same String is return by using String::intern():
String s1 = new String("HELLO").intern();
String s2 = new String("HELLO").intern();
System.out.println(s1 == s2); // will print "true";
Ideone demo
In case of String literal,before creating new Object in String Constant Pool ,JVM will check already same Object persist in SCP area or not if yes it will point to same object instead of creating new Object.Hence, below code s1 == s2 is true
String s1 = "HELLO";
String s2 = "HELLO";
System.out.println(s1 == s2); // true
but we are creating new object by using new keyword, it will create object in heap area, hence s1 and s2 are pointing to two different object, hence it is return false
== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they are logically "equal").
from here How do I compare strings in Java?
== compares the objects reference pointer. When 2 objects are same exact object it will be true.
Instantiating a string using double quotes uses the string pool, creates a string once and reuses it.
Instantiating a string wit new always creates a brand new string.
== tests for reference equality (whether they are the same object).
First Case
System.out.println(s1 == s2); // true
Because you are comparing literals that are interned by the compiler and thus refer to the same object. Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions - are "interned" so as to share unique instances, using the method String.intern.
Second Case
System.out.println(s1 == s2); // false
You are comparing the Object reference which is different so you are getting false.
Please check https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28
i first case u comparing two strings with its ASCII values. thats why...//true
and in second case you are comparing two functions/methods. thats why... //false
the first one is true because s1 and s2 refer to the same string literal in the method area, the memory references are the same. ( == checks just the references in string). when the same string literal is created more than once, only one copy of each distinct string value is stored.
the second one is false because s1 and s2 refer to two different objects in the heap. different objects always have different memory references.
This question already has answers here:
why '==' is returning false even after my hashcode value is same
(3 answers)
Closed 5 years ago.
I read that strings declared as literals are created on String Constant Pool
String s1 = "Hello";
String s2 = "Hello"; -> This will not create a new object and will refer to the s1 reference.
And strings declared with new keyword are created on both Heap Memory and String Constant Pool
String s3 = new String("Hello"); -> This will create a new object in the heap.
But will it create a new constant in the String Constant Pool also or will it use the one from s1?
I have this following code.
Hashcode for all s1, s2, and s3 are return as same.
public class question1 {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3); // not sure why false result, s3 will create a separate constant in the pool? There is already a constant with value "Hello" created by s1. Please confirm if s3 will again create a constant.
}
}
I understant that == compares the object.
Are there two "Hello" defined in the String Constant Pool, one from s1 and one from s3?
String literals are automatically "interned," which places them in a pool. This minimizes the number of instances required. So, the two literals use the same String instance. hashCode() operates on the contents of the String in a consistent way. If two String instances have the same characters, then they will have the same hash code.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
String comparison confused
(3 answers)
Closed 6 years ago.
Javadoc says that if in the string pool there is an equal String that the intern() method will return the String.
public class Demo {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = new String("Apple");
System.out.println(str1.intern() == str2); //false
System.out.println(str1 == str2.intern()); //true
}
}
I expected to get true in both cases.
System.out.println(str1.intern() == str2); //false
In the above case you are comparing reference of interned String "Apple" with that of reference of another String which is on the heap (but with the same value). So, the result is "false".
System.out.println(str1 == str2.intern()); //true
In the above case, you are comparing a reference of String constants pool to a reference got by trying to add "Apple" to the String constants pool. SInce, "Apple" is already in the first line, this interning will return the object pointed to by str1. Hence you get true.
PS : This behavior is described in the javadoc
Doing this str1.intern() is bringing nothing to your try...
Since str1 is a literal string (it will be inmediatly placed in the string pool), and you are comparing it against the str2 which is allocated on the Heap...
therefore
System.out.println(str1.intern() == str2);
will print false, you are comparing the reference of a string in the stirng pool vs a string on the heap...
String.intern() method is used to create an exact copy of heap String object in String constant pool. The String objects in the String constant pool are automatically interned but String objects in heap are not
System.out.println(firstString.intern() == secondString);//false because it will fetch exact copy of the string compare in string
System.out.println(firstString == secondString.intern());//True because it will compare firstString with fetch the second String it will check heap or not
public static void main(String[] args) {
String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1);
String str2 = new StringBuffer("ja").append("va").toString();
System.out.println(str2.intern() == str2);
}
Results:
true
false
First one prints true, and the second prints false. Why are the results different?
The difference in behavior is unrelated to the differences between StringBuilder and StringBuffer.
The javadoc of String#intern() states that it returns
When the intern method is invoked, if the pool already contains a
string equal to this String object as determined by the equals(Object)
method, then the string from the pool is returned. Otherwise, this
String object is added to the pool and a reference to this String
object is returned.
The String created from
String str2 = new StringBuffer("ja").append("va").toString();
is a brand new String that does not belong to the pool.
For
str2.intern() == str2
to return false, the intern() call must have returned a different reference value, ie. the String "java" was already in the pool.
In the first comparison, the String "计算机软件" was not in the string pool prior to the call to intern(). intern() therefore returned the same reference as the one stored in str2. The reference equality str2 == str2 therefore returns true.
Because your assignments don't re-read from the intern pool and Java String(s) are immutable. Consider
String str1 = new StringBuilder("计算机").append("软件").toString();
String str1a = new String(str1); // <-- refers to a different String
str1 = str1.intern();
str1a = str1a.intern();
System.out.println(str1a == str1);
String str2 = new StringBuffer("ja").append("va").toString();
String str2a = new String(str2); // <-- refers to a different String
str2 = str2.intern();
str2a = str2a.intern();
System.out.println(str2a == str2);
The output is (as you might expect)
true
true
Lots of answer before mentioned about the pool and explained really clearly with the Oracle link docs.
I just would like to point out the way we can check when debugging code.
String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1);//the str1.intern() returns the same memory address the str1
String str2 = new StringBuffer("ja").append("va").toString();
System.out.println(str2.intern() == str2);//the str2.intern() does not return the same memory address the str2
You can use any IDE and debug to check the actual address that the str1 and str1.intern()/str2 and str2.intern().
let me add something more interesting:
the OpenJDk 8 is true,true;
Oracle JDK 6 is true,true;
so I think the right answer is :
diffrent vendor's jvm or jvm versions may have diffrent implemention(the language specification don't force the how to)
In Oracle JDK 8(I guess u using):
String “java” already in pool(loaded by java.lang.Version#laucher_name) and String pool only stores the refrence,not the object.
But in OpenJDK the laucher_name is "openJDK";In Oracle JDK 6 and below ,the string pool will copy the string object to itslef.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
if (s1 == s2) is giving true
while (s3 == s4) is giving false.
Can somebody give a detailed explanation onto what is String pool, heap, how many objects are created in each line and how many objects created in total.
Why s3==s4 is giving false?
A detailed explanation will be much appreciated.
When you do new String(...), it is evaluated at runtime and hence creates two different instances and you end up getting s3 == s4 as false. Same thing happens when you use StringBuilder and do something like sb.toString() which is again evaluated at runtime.
For Example,
StringBuilder sb = new StringBuilder();
String foo = sb.append("abc").toString();
String bar = new String("abc");
String foobar = "abc";
Here foo, bar and foobar are all different objects and hence foo == bar or foo == foobar will evaluate to false.
On the other hand s1 == s2 returns true because abc one declared, already exists in the String Pool and in this case both the objects points to the same reference in the pool.
String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.
Strings are developed to offer operations with many characters ans are commmonly used in almost all the Java applications
Some interesting facts about Java and Strings:
String in immutable and final in Java and in this case JVM uses String Pool to store all the String objects.
What are different ways to create String Object?
We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.
There are too several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilderetc etc.
To your Question:
When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.
So when you do
String s1 = "abc";
String s2 = "abc";
those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.
but when you do:
String s3 = new String("abc");
String s4 = new String("abc");
you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.
Please take a look at the image below for a more illustrative example.