Data comparison of Java learning

Here is a summary of some common comparisons and their differences:

Comparison of the equal sign "=="

Use "==" to compare common basic data types, and compare the memory addresses of variables.

Object's equals() method

The equals() method in the Object class is similar to "==", and the comparison is also the memory address of the variable.

Comparison method in String
  1. str.equals(String otherstr) Returns true
    if the two strings have the same characters and length, when compared using the equals() method.
  2. str.equalsIgnoreCase(String otherstr)
    ignores case and returns true if the two strings have the same characters and length.
  3. str.compareTo(String otherstr)
    This method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the string. If the String object is before the parameter string, the result of the comparison is a negative integer; order This String object comes after the parameter string, the result of the comparison is a positive integer, and if equal, the result is 0.
  4. Data comparison of several wrapper classes (Integer, Boolean, Byte, Character, Double, Number)
public class Equal {
    public static void main(String[] args) {
        String str1=new String("123");
        String str2=new String("123");
        String str3="123";
        String str4="123";
        String str5=new String("12"); 
        double e=1.23;
        double f=1.23;
        Integer int1=new Integer(10);
        Integer int2=new Integer(10);
        Integer int3=new Integer(9);
        //String类型下的比较
        System.out.println(str3==str4);
        System.out.println(str1==str2);
        System.out.println(str2.equals(str3));
        System.out.println(str2.compareTo(str1));
        System.out.println("-----------");
        //基本类型的比较
        System.out.println(e==f);   
        System.out.println("-----------");
        //包装类的比较
        System.out.println(int1==int2);
        System.out.println(int1.equals(int2));
        System.out.println(int1.compareTo(int2));
        System.out.println(int3.compareTo(int2));
        //改进str2
        str2=str2.intern();
        System.out.print("改进后的str2和str3的比较:");
        System.out.println(str2==str3);
    }
}

The result is:

true
false
true
0
-----------
true
-----------
false
true
0
-1
改进后的str2和str3的比较:true

Here is a little explanation of the code:

Generally, when we define the shape like
String str3=”123”;
String str4=”123”;,
the “123” defined for the first time will be put into the string pool. When str4 is defined again, this is the first time it will be defined. To find out whether "123" already exists in the string pool, if so, return the address reference of "123" in the string pool, so str3==str4 is also established.

Obviously, the variables defined by the general basic type can be compared directly by "==", but for String and the data wrapper class Integer, etc., "==" compares only the value of the object reference, and the value of the object reference (that is, the pointer). The value of ) is the address of the object, which is stored in the stack memory, while the actual object is stored in the heap memory, and the address contained in the reference is the heap address of the object. Every time the object is new, it will apply for a new address space, even if the space content is the same, but the applied address is different, so when using "==" for comparison, the result cannot be obtained correctly. Therefore, classes such as String and Integer override the equals() method of the parent class Object, so that the contents of the objects can be compared, but in the Object class, the equals() method is actually equivalent to "==".

Among them, classes such as String and Integer also contain the compareTo method, which can effectively compare the size and draw the corresponding conclusion. For the introduction of the compareTo method, please refer to the API

Finally, let's talk about the intern() function. The following is an introduction to this function in the API.

When the intern method is called, if the pool already contains a string equal to this String object (as determined by the equals(Object) method), the string in the pool is returned. Otherwise, add this String object to the pool and return a reference to this String object.

It can be seen that when the intern method is called, the value of str2 is the address value of "123", so str2==str3 is also established.

For an introduction to the stack in Java, you can refer to the following link for a
detailed explanation of the Java stack

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325972297&siteId=291194637