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

(zz) 我为旅行狂---Key West亲情计划 (2009/5/5)

强烈建议各亲朋好友在离开南部之前,到美国最南端的小岛上看一看。体验一下海天相接的极致景致,以及阳光灿烂的明媚心情。给在南部生活的学术生涯点缀一些不一样的心情,留下值得回忆的美好。此次 KW 之行历时 6 天,驱车 38 小时,经过 Pensacola , Miami , Key Largo , Key west , Plam Beach, 以及 Everglades. 除了预计到的长途跋涉辛苦以及晒成麦色的皮肤之外,可以用圆满来形容。特此感谢 YC MM ,其先驱之行起到了至关重要的影响意义。在脑海里只留下一片片的海蓝色之前,先奉献上我们的驱车路线,以便感兴趣的驴友们方便之用。 提前 10 天的时候记得先查天气,千万不要遇到 hurricane 这样的危险情况,这对海边游历是个大忌。我们此次采取西下东上。一路晴空万里,运气甚为好。               4.24           早 6 点出发, 4 个半小时之后到达 pensacola beach. 这片海域的确与众不同,若是以前没有去过,千万不要错过。雪地一般的沙滩,宝石一般的海水,一望无际的天空。最妙的在于那是狭长的海岸,我们 停下车的地方就是小小海道的尽头,像是走进了海的深处,妙不可言。若是允许,最好带上透风较好的帐篷,海风中安睡的感觉值得体验。下午 6 点左右吃过饭,继 续向南前行大概 2 个小时,到达一个不知名小镇安住下来,目的是给明天的 miami 路途节省时间和精力。           4.25           早 9 点出发,史进 Miami 。这段距离大概需要 8 个小时。一路上风景一般,不过还是和家里不同。成片的椰树林也是很养眼的。到达 Miami 的时候已是日暮。若是想去大名鼎鼎的 joy's stone crab 吃著名的 stone crad, 一定记得先去餐厅定上位子。他们生意做的很牛,若是刚好赶上周末,是不给电话定位的。所以要 physically 先去,定上时间。然后去天体浴 场看看裸体的人们以及美丽的日落。差不多华灯...

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...

Changing Task Progress By SwingWorker

When SwingWorker is used to report task progress, it should be noted that the propertyChangeEvent won't be invoked if the oldValue is the same as the oldValue. To solve this problem, it is better to reset your progress status to zero or random number before the SwingWorker starts. The setting will force the propertyChangeEvent to be invoked. So your progress status will be updated.