This question already has answers here:
What is Java String interning?
(8 answers)
Closed 6 years ago.
How does Java decide when a new String should be created?
I assume it depends on the specific JVM implementation/ compiler, but I thought the results of this test were interesting enough to ask the question:
class A { public String test = "test";}
class B { public String test = "test";}
...
public static void main (String[] args)
{
String test = "test";
System.out.println(test == "test"); // true
System.out.println(test == new String("test")); // false
System.out.println(new A().test == new B().test); // true
}
String literals are always interned, this is not directly platform-dependent.
Except when using the explicit initialization idiom through the constructor (new String(someString)).
In that case, a new Object is created, which causes the reference equality operator (==) to return false in your example.
Needless to say, always use equals for reliable String value comparison, but I suspect this falls out of your question's scope.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
how to check reference equality in an object which implements content equality?
(5 answers)
Closed 4 years ago.
public class Test
{
public static void main(String[] args)
{
String s = new String("test");// ***
String s1 = s.toUpperCase();
String s2 = s.toLowerCase();
String s3 = s1.toLowerCase();
System.out.println(s==s1);//first case
System.out.println(s==s2);//second case
System.out.println(s==s3);//third case
}
}
1) Why does it return false for the third case(commented). Both s3 and s are pointing test yeah? But it seems JVM creates another object named test for s3 in the heap memory. But it is not the same for the second case(commented). It uses the object which was previously created as s(commented as *** in the code). Why is that?
2) And what happens to s1 object TEST because s3 is created from s1. Will s1 be destroyed or will it be in heap?
Here String s = new String("test"); object will be created in heap area (not inside String pooled area) but any other string returned after any operation will be created in String pooled area. To answer your question:
s==s3 returned false as they are two different objects.
s1 won't be collected by garbage collector until it is being referenced.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
How many Strings are created in memory?
(4 answers)
Closed 4 years ago.
How does Java implement the below string comparisons
public class MyClass {
public static void main(String args[]) {
String a = "Chaipau";
String b = "pau";
System.out.println(a == "Chai"+"pau"); //true
System.out.println(a == "Chai"+b); //false
}
}
This is different from How do I compare strings in Java? , as the answer does not contain the why a new object is created in the second case , when it could have pointed to the same object reference as it is done in the first case.
"Chai"+"pau" is semantically identical to "Chaipau", and thus is the same instance that a refers to.
"Chai"+b is evaluated at runtime (because b is not a compile-time constant expression), creating a new instance of String, and thus is not the same instance that a refers to.
This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 4 years ago.
I am running this program. To check string pool concept. I refer this link and added two more lines with equals method from Object class in java. Why I am getting different output for objRef1==objRef2 vs objRef1.equals(objeRef2). In my opinion they should be same.
public class Abc2 {
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");
System.out.println("s1 == s2 :"+(s1==s2));
System.out.println("s1.equals(s2) :"+(s1.equals(s2)));
System.out.println("s1 == s3 :"+(s3==s2));
System.out.println("s1.equals(s3) :"+(s3.equals(s2)));
}
}
String is a complex datatype so == find two different refetences.
Strings equals() compares the content instead of the reference.
So two "Cat"s has the same content but arent the same object.
Its like you try to say C:\test.txt and C:\test2.txt where the same file because they have the same content.
This question already has answers here:
Use String hashCode() Method? [closed]
(4 answers)
Closed 4 years ago.
Consider the piece of code given below.
I was wondering how can a string present in string pool "s1" or "s2" have the same hashCode as a string present in heap as "s3" but outside string pool.
class Test{
public static void main(String[] args) {
String st1 = "Shiva";
String st2 = "Shiva";
String st3 = new String("Shiva");
System.out.println(st1 == st2);
System.out.println(st1 == st3);
System.out.println(st2 == st3);
System.out.println(st1.hashCode());
System.out.println(st2.hashCode());
System.out.println(st3.hashCode());
}
}
input: deep (master *) LanguagePackageInJava $ javac Lecture3.java
output: deep (master *) LanguagePackageInJava $ java Test
true
false
false
79855167
79855167
79855167
I have searched a lot regarding this question. Please do tell me where am I wrong in my thought process.
hashcode and equals method have contract , so if two string are true for equals then their hashcode should be same.
This question already has answers here:
will two strings with same content be stored in the same memory location?
(9 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
As the java-doc tells Java Operator == tests for reference equality (whether they are the same object). so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.
But while running a piece of code all i found is that this statement doesn't satisfy the Output of the code.
Here's the code:
public class Test2 {
public static void main(String[] args)
{
String s="Sachin";
String t="Sachin";
System.out.println(s==t);
}
}
And Surprisingly i found a output "true".
Please Help me understand why it is so?
Here's a Screenshot to my program output:
https://i.stack.imgur.com/OZ0PW.jpg
You assuption is that
String s="Sachin";
String t="Sachin";
creates two string objects, but this is not true.
Java optimises the usage of string so that is puts literal strings in the string pool so that it assinges an already created string object from that pool if the compiler finds the same string a second time. This is called string interning.
You better try this:
public class Test2 {
public static void main(String[] args)
{
String s="Sachin";
String t=new String(s);
System.out.println(s==t);
}
}