| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
What are the differences between the equality operator and the equals method?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
What are the differences between the '==' operator and the equals() method?The Java language offers the programmer more than one way to check for equality -- but all those ways are not created equal. The equality operator == is a fundamental operator in the Java language. The result type of the expression is always boolean. According to 15.21 Equality Operators in Java Language Specification 3rd Edition:
In contrast, the equals() is an instance method which is fundamentally defined by the java.lang.Object class.
This method, by convention, indicates whether the receiver object is "equal to" the passed in object. According to The equals method implements an equivalence relation on non-null object references:
The equals() method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). Other classes, including those you write, may override this method to perform more specialized equivalence testing. Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
The typical problem for most people is in using == to compare two strings (for example, For String Object:
For Example:
public class Program {
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println("str1 == str2 is " + (str1 == str2));
System.out.println("str1.equals(str2) is " + (str1.equals(str2)));
}
}
The output is str1 == str2 is false str1.equals(str2) is true
Strings are immutable and their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
The String manipulation methods take the source string as input and return an new string as output if there are any changes, otherwise the original string will be returned.
Here is the
public String substring(int beginIndex, int endIndex)
{
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex)
throw new StringIndexOutOfBoundsException();
if (beginIndex == 0 && endIndex == count)
return this; //No Changes
int len = endIndex - beginIndex;
// Package constructor avoids an array copy.
return new String(value, beginIndex + offset, len,
(len << 2) >= value.length); //Generate a new String
}
Let's make an example for using substring function:
class Program {
public static void main(String[] args) {
String s = "ABCDEFG";
String s1 = s.substring(0, 4);
String s2 = s.substring(0, 4);
String s3 = s.substring(0, s.length());
System.out.println("s1 == s2 is " + (s1 == s2));
System.out.println("s == s3 is " + (s == s3));
}
}
The output is s1 == s2 is false //Creates two new String objects s == s3 is true //Returns the original String Object To save space and reduce complexity, JVM uses a concept of string constant pool. For example:
public class Program {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
System.out.println("str1 == str2 is " + (str1 == str2));
System.out.println("str1.equals(str2) is " + (str1.equals(str2)));
}
}
The output is str1 == str2 is true str1.equals(str2) is true Why? Please read What is String Literal Pool? in XyzWs Java FAQ.
|