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

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.

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.

How to Access Variable's Memory Address In Java? [duplicate]

This question already has answers here:
hashCode uniqueness
(7 answers)
What is an object's hash code if hashCode() is not overridden?
(12 answers)
Does hashcode number represent the memory address? [duplicate]
(9 answers)
How can I get the memory location of a object in java?
(4 answers)
Closed 3 years ago.
I wrote this code and want to see memory location of 2 objects that i create from one class and make instance of one specific variable.
public class StaticFields {
int a = 12;
int b = 235;
public static void main(String[] args) {
StaticFields obj = new StaticFields();
int a = obj.a;
StaticFields obj2 = new StaticFields();
int c = obj2.a;
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(c));
}
}
why the "identityHashCode" of "a" and "c" is the same ?
Thanks.
Both the integers carry the same value, 12.
Since Integers are cached (for values up to 127 from -128), the hash code value of both objects returned are same.
This is not true for b since its value is greater than 127.

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

String basic query [duplicate]

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

Categories