Skip to main content

Comparison == vs equals() in Java

In java, we have two operations ("==" and "equals()") to check the equivalence of Strings. It is important to understand that the difference between them. 
 
==
The == operator compares the two objects and checks to see if two objects are exactly the same object (refer to the same instance).

.equals()
The equals() method compares the characters in the two different objects. Two strings may be different objects, but can contains the same values (exactly the same characters).

public class Main {

    public static void main(String[] args) {

        String s1 = "Shangshu";
        String s2 = new String(s1);

        System.out.println(s1 + " == " + s2 + " is " + (s1 == s2));
        System.out.println(s1 + ".equals(" + s2 + ") is " + (s1.equals(s2)));
    }

}

The output of the above code is:
Shangshu == Shangshu is false
Shangshu.equals(Shangshu) is true


However, please compare the following code segment with the above code:
public class Main {
    public static void main(String[] args) {

        String s1 = "Shangshu";
        String s2 = "Shangshu"; //new String(s1);

        System.out.println(s1 + " == " + s2 + " is " + (s1 == s2));
        System.out.println(s1 + ".equals(" + s2 + ") is " + (s1.equals(s2)));
    }
}

The output of this code will be:
Shangshu == Shangshu is true
Shangshu.equals(Shangshu) is true

It is caused by the JVM optimization. When JVM detect two constant strings with the same content, it will optimize the the two constant strings to one copy to save memory. Therefore, the two string references will refer to the same object.

Comments

Popular posts from this blog

Oracle ORA-01000 maximum open cursors exceeded

Several days ago, I wrote a piece of program in C#/.Net to import some records into oracle database. It is windows version 10g XE. When the program is running, I got a run-time error which is ORA-01000 maximum open cursors exceeded. Here is the Oracle docs about ORA-01000: ORA-01000 maximum open cursors exceeded Cause: A host language program attempted to open too many cursors. The initialization parameter OPEN_CURSORS determines the maximum number of cursors per user. Action: Modify the program to use fewer cursors. If this error occurs often, shut down Oracle, increase the value of OPEN_CURSORS, and then restart Oracle. In order to solve this problem, I increased my OPEN_CURSORS by using: ALTER SYSTEM SET open_cursors = 5000 SCOPE=BOTH; You can use SQL/PL client and "connect" command to login the database system by using "system" user to change the open_cursors. If you are using .NET, DBA, and Java programming, please remember to close all the re...

Tips about sizeof() function in C and C++

The unary operator sizeof is used to calculate the sizes of datatypes in Bytes in modern luanguages in the programming languages C and C++. It is an useful operation in memory allocation. In order to use it right, some tips that you may want to know: sizeof() is a compile-time function (macro-like function), not a run-time function. Therefore, you can declare array as: int arr[sizeof(int)]; because sizeof() is a compile-time function, the equation in sizeof() function will not be calculated. For example: int i=3; int a = sizeof(i++); the value of i will not be changed after sizeof(i++);  because sizeof() is a compile-time function, it can not help you determine the size of an array parameter. The following code will print out size 12 and 4. void test_sizeof(int arr[]) { cout << "sizeof(arr) = " << sizeof(arr) << endl; } int main() { int arr[3]; cout << "sizeof(arr) =" << sizeof(arr) << endl; te...