Difference in the results of String comparison in Java? [duplicate] - java

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.

Related

Why I am getting different output for string pool while comparing two objects in java? [duplicate]

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.

Does Java Operator "==" really compare two objects based on memory reference? [duplicate]

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

String reuse in Java [duplicate]

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.

Java "abc" == "abc" operation [duplicate]

This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
Closed 7 years ago.
When we do:
String a = new String("abc");
String b = new String ("abc");
and do a a == b it returns false because they are 2 diferent objects.
But when we have this:
String c = "abc";
String d = "abc";
and we do a c == d it returns true. Why is that? Should it return false also? Why does the == operator behaves as a .equals() method in this case?
This happens because Java uses a so called Stringpool and tries to reuse old String-Literals to save some memory. But if you say "new String" you always create a new Object based on the Literal. See: here I would suggest you to always use the Objects.equals(a, b) if you want to make sure the objects are equal (or call equal on the Object itself if you're sure it's not null)

What's wrong with Integer, Java? [duplicate]

This question already has answers here:
Integer wrapper objects share the same instances only within the value 127? [duplicate]
(5 answers)
Wrong output for integer comparison values
(2 answers)
Why is == true for some Integer objects? [duplicate]
(4 answers)
Closed 9 years ago.
public class Test {
public static void main(String[] args) {
Integer i=555,j=555;
System.out.println(i==j); //false
Integer l=5,n=5;
System.out.println(l==n); //true
}
}
Why, Java? How is that even possible?
You're comparing the references of two different Integer class instances with the same value, so you must use the equals method (as it must be to compare equality between objects):
Integer i=555,j=555;
System.out.println(i==j); //false
Integer i=555,j=555;
System.out.println(i.equals(j)); //true
But Integer has a pool of Integer object instances for int values between -128 and 127. So when you do
Integer l=5,n=5;
System.out.println(l==n); //true
You receive true since l and n points to the same object reference.

Categories