Home > Java > JavaBase > body text

String identity judgment in java

王林
Release: 2019-11-29 15:29:28
forward
2376 people have browsed it

String identity judgment in java

1. Cause: String identity judgment

//String is reference type
String str1 = new String("hello");
String str2 = new String("hello");
 
System.out.println(str1==str2);
// false
System.out.println(str1.equals(str2));
// true
Copy after login

Online learning video sharing:java在线learning

2. Relational operator “==”

Points: "==" can only process the identity judgment of the corresponding value of the basic data type, and is not applicable to the reference data type (the value of which is stored in the address)

public class Main {
    public static void main(String[] args) {
        int n=3;
        int m=3;
// true
        System.out.println(n==m);
 
        String str = new String("hello");
        String str1 = new String("hello");
        String str2 = new String("hello");
// false
        System.out.println(str1==str2);
 
        str1 = str;
        str2 = str;
// true
        System.out.println(str1==str2);
 
    }
}
Copy after login

n==mThe result is true, this is easy to understand. The values ​​stored in variable n and variable m are both 3, which must be equal. And why are the results of the two comparisons of str1 and str2 different? To understand this, you only need to understand the difference between basic data type variables and non-basic data type variables.

8 basic data types in Java

Floating point type: float, double; Integer type: byte, short, int, long; Character type: char; Boolean type: boolean.

For these eight basic data types of variables, the variables directly store the "value", so when comparing using the relational operator ==, what is compared is the "value" itself. It should be noted that floating point and integer types are signed types, while char is an unsigned type.

For variables of non-basic data types (reference types), such as String type , a reference type variable stores not the "value" itself, but the address in memory of the object associated with it. For example, str1 is not the directly stored string "hello", but the address corresponding to the object.

So when you use == to compare str1 and str2 for the first time, the result is false. Because they point to different objects respectively, that is to say, the memory addresses where they are actually stored are different. In the second comparison, str1 and str2 point to the object pointed to by str at the same time, so the result is undoubtedly true (the address is the same).

3. Object method "equals()"

The equals method is a method in the base class Object, so all classes that inherit from Object will have this method . In the Object class, the equals method is used to compare whether the references of two objects are equal, that is, whether they point to the same object.

public class Main {
    public static void main(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
// true
        System.out.println(str1.equals(str2));
 
    }
}
Copy after login

The String class overrides the equals method to compare the pointed strings Whether the strings stored in the object are equal. Some other classes, such as Double, Date, Integer, etc., have overridden the equals method to compare whether the contents stored in the pointed objects are equal.

Summary:

For ==: If it acts on a variable of a basic data type, directly compare whether its stored "value" is equal; if it acts on a reference type variables, the address of the object pointed to is compared

For the equals method: If the equals method is not overridden, the address of the object pointed to by the reference type variable is compared; such as String, If classes such as Date override the equals method, the contents of the pointed objects are compared (the equals method cannot act on variables of basic data types).

For more related articles and tutorials, please visit: Getting Started with Java

The above is the detailed content of String identity judgment in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!