This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
Closed 5 years ago.
if (responseEntity.getBody().getMeta().getCode() != ApiExceptionEnum.SUCCESS.code()) {
return null;
}
code like this,
responseEntity.getBody().getMeta().getCode() -> Integer 1
ApiExceptionEnum.SUCCESS.code() -> Integer 1
sometimes it will return null !!
why ?
the response is from redis
In this case == or != checks whether compared objects are pointing to the same place in memory. To compare values stored in compared objects, use .equals() method inherited by all Java objects from Object class.
Related
This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 3 years ago.
I am new to Java and StackOverflow but as i have read some of the answers about equals they stated :
Equals method compares two objects for their identity and if they are
identical it returns TRUE . whereas if you don't override the method Equals
it acts like ==(which returns true if 2 variables refer to the same object).
Integer x = new Integer(4);
Integer y = new Integer(4);
System.out.println(x.equals(y));
System.out.println(x == y);
If the queries above are correct why does this code print TRUE and FALSE since we are not overriding the method equals?
Because class Integer does override the equals method and it's implementation is the following:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
This question already has answers here:
Why are 2 Long variables not equal with == operator in Java?
(3 answers)
Weird Integer boxing in Java
(12 answers)
Closed 5 years ago.
public class MyClass
{
public static boolean isEqual(Object obj1, Object obj2)
{
return obj1==obj2;
}
public static void main(String args[])
{
//output: true
System.out.println(2.2f==2.2f);
//output: false
System.out.println(isEqual(2.2f,2.2f));
//output: true
System.out.println(22L==22L);
//output: true
System.out.println(isEqual(22L,22L));
}
}
All of the following print statements except the second output true. Why is this so? The method isEqual() call for two integer literals outputs true yet the call for two floating point literals outputs false, but why? And why does this operates differently from the regular == comparison?
Your method isEqual() expects Objects as arguments. So the primitive longs and floats are boxed, and the created Long/Float instances are passed as argument. This internally calls the methods Long.valueOf()/Float.valueOf().
Your implementation of Long.valueOf() caches some values and returns cached values (it's allowed, but not required to do so, unlike Integer.valueOf() which is required to return cached values for the range -128 to 127)). So Long.valueOf(22L) == Long.valueOf(22L). That's just not the case with Float.valueOf().
Try with larger long values, and the comparison will be false.
You should never compare objects using ==, except if they are enums. Use equals().
This question already has answers here:
Comparing primitive to wrapper object with == behaviour unexplained
(4 answers)
Closed 6 years ago.
output are: true,true*,false*
what is going on case=1 and case=2
if case:1 is true than why? value 5 has different memory allocation?
we know that '==' operator compare based on memory or refrence
Integer a=new Integer(5);
Integer b=a;
System.out.println(a==b); //true i know
/*case:1 */System.out.println(a==5); //true? why
/*case :2 */ System.out.println(a==new Integer(5)); // false ? why
Check java doc for Integer https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
Correct way of comparing objects is using equals
public boolean equals(Object obj)
Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.
Overrides:
equals in class Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
For case 2, the Integer a is auto-unboxed yielding the primitive 5, which is equal to 5.
For case 3, a new Integer object is created, which has a different address to that equality cannot be checked via ==. You'd have to use the equals-method in this case: (a.equals(new Integer(5)) is true)
This question already has answers here:
why equals() method when we have == operator? [duplicate]
(8 answers)
Closed 8 years ago.
<br><% if (overLimit != null) {%><%=overLimitItemNO.getClass().equals(shoppingVO.getItem_no().getClass())%><%}%>
<br><% if (overLimit != null) {%><%=overLimitItemNO.getClass()%> vs <%=shoppingVO.getItem_no().getClass()%><%}%>
<br><% if (overLimit != null) {%><%=overLimitItemNO%> vs <%=shoppingVO.getItem_no()%><%}%>
<br><% if (overLimit != null) {%><%=overLimitItemNO == shoppingVO.getItem_no()%><%}%>
and it shows the following on the browser
true
class java.lang.Integer vs class java.lang.Integer
300008 vs 300008
false
You need to use obj1.equals(obj2). == on objects only compares whether the two are the same exact instance
Use .equals when comparing Integer's otherwise you are comparing object refs.
This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When we do
String a=new String("mac");
String b=new String("mac");
if(b == a)
{
System.out.println("condition 1 is true");
}
if(b.equals(a))
{
System.out.println("condition 2 is true");
}
condition 1 fails and condition 2 is true since b and a are two different objects
But when we do
String a="mac";
String b="mac";
if(b == a)
{
System.out.println("condition 1 is true");
}
if(b.equals(a))
{
System.out.println("condition 2 is true");
}
Both the conditions are true. Why didn't java create a new object for second case. If java creates a new object only when we use new() then if we give different values to both the strings then what happens internally in java?
When you declare like below, Java create String literals in the String constant pool, and both references a and b will refer that String object "mac" in the pool
String a="mac";
String b="mac";
So, both == and .equals() returning true.
But, when you create String object by using new operator, String objects are created in the heap like other regular objects in java.
So == operator will return false, since both references referring two different object in the heap.