Kamis, 17 Juli 2014

Java Mistake 3 - Using "==" instead of equals() to compare Objects in Java




In this part of Java programming mistakes, we will take a look on another
common pattern, where programmers tend to use
"==" operator
to compare Objects, similar to comparing primitives. Since equality of object
can be very different in physical and logical sense, and in case of domain
objects it's mostly driven by business rules, comparing objects with
"=="
operator, introduces subtle bugs, which are hard to find. Difference between equals() and ==
operator
,  one of
the Java
classics is also asked to find out if developer is familiar with this important
concept or not. Using == operator only make sense when comparing primitives
like int, or final constants like
Enum. Though
there is more involve in comparing two Enum, which you
learn by following that link. One of the most common pattern of this mistake is
is comparing two Strings with == operator, which we will see in this article.
By the way this is third in series of common Java programming mistakes, and if
you haven't read the previous two, you can read them here :










Comparing Strings with == instead of equals()
method





Common Java Mistake bad practice by Java programmersChecking if two Strings are equal or not using equality operator (==)  and not with equals method is a mistake,
mostly seen between fresher or new Java developers. What makes it worst is the
fact that
String literal if
compared with equality operation
(==) or not
equality operator
(!=) behaves perfectly, which makes programmers
think that, this is the right way of comparing Strings in Java, which in fact
is not. In reality  "=="
operator returns true, if both operands point to same object. In case of String
literal it’s true because
String pool returns same String object if
created as literal, but it breaks when you compare two
String which are
not in pool or only one of them is literal. To avoid this subtle bug, always
use equals() method of String to
comp
are equality of  two
String in Java,
which returns true if both string objects contains exact same characters. Here
is an example of comparing
String which makes this Java mistake
clear:

public class UseEqualsToCompareString {

public static void main(String args[]) throws IOException {
String name = "abc";
String surname = "abc";

//this will return true because name and surname points to same string object
if(name == surname){
System.out.println("Two Strings are equal by == because they cached in string pool");
}

surname = new String("abc");

//this will result in false as now surname is not a string literal and points to different object
if(name == surname){
System.out.println("String literal and String created with new() are equal using ==");
}else{
System.out.println("String literal and String created with new() are not equal using ==");
}

//both strings are equal because there content is same
if(name.equals(surname)){
System.out.println("Two Strings are equal in Java using equals method because content is same");
}else{
System.out.println("Two Strings are not equal in Java using equals method because content is same");
}

}

}
Output:
Two Strings are equal by == because they cached in string pool
String literal and String created with new() are not equal using ==
Two Strings are equal in Java using equals method because content is same





So always use equals() method to compare two strings in
Java. This is also true for comparing instance of Integer class in Java 5. Java
5 introduces autoboxing and unboxing which
converts
int to Integer automatically. worst part is that auto boxing creates
Integer object by calling
Integer.valueOf() method which maintains an
internal Integer cache of default range -128 to 128. So anytime you autobox an
int value which is form
-128 to 127 it will return same Integer
object which if compared with equality operator
"==" will
return true, but as soon as you moved out of that range auto boxed integer
compared with "==" will return false despite of having same numerical
value. see for more. This creates lot of confusion around Java programmers.
Bottom line is avoid using
"==" for
comparing object until you want it explicitly, always use equals() method for checking
equality of two objects . Following Java code example will make these things
clear.

public class IntegerAutoBoxingTest {

public static void main(String args[]) throws IOException {
Integer number1 = 12;
Integer number2 = 12;

//two integers will be equal because Java maintains cache of Integers for values -128 to 128
if(number1 == number2){
System.out.println("Two Integers objects are equal with == because they created "
+ "using auto boxing and there value is between -128 to 128");
}

number1 = new Integer(12);

//two integers will not be equal because one new Integer() creates separate object
if(number1 == number2){
System.out.println("Integer created using auto-boxing and created using new() "
+ "are equal using == ");
}else{
System.out.println("Integer created using auto-boxing and crated using new() "
+ "are not equal using == ");
}

//both Integers will be equals because there numeric value is same
if(number1.equals(number2)){
System.out.println("Two Integers in Java are equal using equals method "
+ "because of same numeric value");
}else{
System.out.println("Integers in Java are not equal using equals method");
}

number1 = 150;
number2 = 150;

//Integers will not be equal as there are out of cache range -128 and 128.
if(number1 == number2){
System.out.println("Two Integer objects created with auto boxing with value "
+ "outside -128 to 128 are equal using == ");
}else{
System.out.println("Two Integer objects created with auto boxing with value "
+ "outside -128 to 128 are not equal using == ");
}

}

}







That's all on this part of common Java mistakes. Remember, always
prefer
equals() method over == operator for
checking object’s equality. Let us know if you like this series and have any
other common Java mistake or pattern, which you would like to share with Java
community.





























Source:http://javarevisited.blogspot.com/2013/05/java-mistake-3-using-instead-of-equals.html

1 komentar:

  1. Excellent article. Thanks for sharing.
    https://www.flowerbrackets.com/difference-between-equal-operator-vs-equals-method-java/

    BalasHapus