JAVA learning (compare the difference between == and the equals of the object-oriented)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43271086/article/details/91379742

Java program to test two variables are equal, there are two ways: one is to use == operator, the other is using the equals method.

When == operator determines whether two variables are equal, if the two variables are the basic types of variables, and the values ​​are equal, the time when the two variables are equal, it returns true.

But for reference variable type variable, only when they point to the same goal when it is equal, equal sign can not be used to pay attention to the object relationships are more like father and son.

Next we look at a piece of code

public class EqualTest {
    public static void main(String[] args) {
        int it=65;
        float f1=65.0f;
        System.out.println("65和65.0f是否相等?"+(it==f1));
        char ch='A';
        System.out.println("65和A是否相等"+(it==ch));
        String str1= new String("hello");
        String str2=new String("hello");
        System.out.println("str1和str2"+(str1==str2));
        System.out.println("str1是否equals str2"+(str1.equals(str2)));
        //System.out.println("hello"==new EqualTest());
    }
}

Please run this will see the results on your computer

Here, I focus on telling the difference between str1 and str2 using == and the equals

String str1= new String("hello");
String str2=new String("hello");

Compare 1:

System.out.println("str1和str2"+(str1==str2));

Operating results: false

The reason: When the two variables were compared, only they point to the same object can output true, so here created two string, belonging to two heap memory area, that is, they are not an object

Compare 2:

System.out.println("str1是否equals str2"+(str1.equals(str2)));

Operating results: true

Reason: equals comparison is one kind of value, irrespective of the position, so the output is true

Compare 3:

System.out.println("hello"==new EqualTest());

The result: not run.

The reason: because there is no inheritance relationship between java.lang.String and EqualTest.

String took in solving the mystery of a very local

Hello difference with the new String ( "hello") of

When the program uses direct string hello amount of time, JVM will manage the strings in the constant pool.

When the program uses the new String ( "hello") is, JVM is created directly in a string constant pool amount, and then create a new object using new String, so it can create two string objects.

Key: JVM constant pool only save a string literal.

Code shows:
 

public class EqualTest {
    public static void main(String[] args) {
        String s1="我是小菜鸡";
        String s2="我是";
        String s3="小菜鸡";
        String s4="我是"+"小菜鸡";
        String s5="我"+"是"+"小菜鸡";
        String s6=s2+s3;
        String s7=new String("我是小菜鸡");
        System.out.println(s1==s4);
        System.out.println(s1==s5);
        System.out.println(s1==s6);
        System.out.println(s1==s7);
    }
}

operation result:

true    true     false     false

After analyzing two cause of the error:

Compare 1:

System.out.println(s1==s6);

The reason: the latter value s6 can not just finalized at compile time.

Compare 2:

System.out.println(s1==s7);

The reason: to create an object using new String () when the operation is carried out at the time, he was kept in the heap memory, and not into the constant pool

Guess you like

Origin blog.csdn.net/weixin_43271086/article/details/91379742