String basic query [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I have the following code:
String s1= "Hi";
String s2="Hi";
String s3=s1.concat(" a");
String s4="Hi a";
System.out.println(s1==s2);
System.out.println(s1=="Hi");
System.out.println(s3.equals(s4));
System.out.println(s3==s4);
Why is System.out.println(s3==s4) false?

This is the difference between == and .equals().
== checks that the references for the two objects are equal
.equals() is implemented by the object to check whether it is equal with another object
== should only be used for primitive types

Related

In java String comparison, how == operator and hashcode() methods are behaving differently [duplicate]

This question already has answers here:
What is the use of hashCode in Java?
(8 answers)
Closed 2 years ago.
In String class hashCode() method is readable format of memory address.
As per the below code == operator compares memory location or hashcodes.
then how == operator returning false and hashCode() method returns true.
public class TestStringEquals {
public static void main(String[] args) {
String s1="Hello World";
System.out.println(s1.toUpperCase()==s1.toUpperCase()); //false
System.out.println(s1.toUpperCase().hashCode()==s1.toUpperCase().hashCode()); //true
}
}
== compares the starting pointer of two objects in Memory.
hascode is like checksum.

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

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.

String concatenation and comparision using == [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
why the result is false?
could someone please explain?
public class StringTest1 {
public static void main(String[] args) {
String a="a";
String b=a+"b";
String c="ab";
System.out.println(b==c);
}
}
Because they don't point to the same object in the memory.
== is used for comparison of either primitive types, or object references.
What you want to do, is to compare their values, for which you 'll need to use the equals(Object o) or equalsIgnoreCase(Object o) method(s)
Output of this comparison is FALSE because you have created two
objects which have different location in heap so == compare their
reference or address location and return false.
Read more: http://java67.blogspot.com/2012/11/difference-between-operator-and-equals-method-in.html#ixzz3xmRzfSkP

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