Home > Java > JavaBase > How to use if statement to determine whether strings are equal in java

How to use if statement to determine whether strings are equal in java

王林
Release: 2019-11-27 11:55:09
Original
17297 people have browsed it

How to use if statement to determine whether strings are equal in java

First analyze and use == (Note: = in Java is the assignment operator, == is the comparison of equality) and the equals() method to compare two The difference between string equality:

== compares whether the addresses of two strings are equal (the same address), and the equals() method compares whether the contents of two string objects are the same (of course , if two strings refer to the same address, comparison using equals() will also return true).

Recommended related learning videos: java video

The following will give two examples to analyze the string comparison problem in the two situations:

Example 1, use the new keyword to declare two String type variables

How to use if statement to determine whether strings are equal in java

When running the program, it is found that comparing with equals returns true, and comparing with == returns false. The reasons are as follows:

Because == compares whether the addresses of two string objects are the same (whether they are the same address). When using the new keyword to create an object, the object occupies a separate storage space. , stores the value of the string llg. Therefore, although the two strings s1 and s2 have the same value, the stored address is not the same address.

When using == to compare, the comparison is whether the addresses of the two strings are the same, so false is returned. But using the equals() method to compare these two strings will compare whether the values ​​​​of the two strings are the same, so it returns true.

Example 2, string declaration and definition under normal circumstances

How to use if statement to determine whether strings are equal in java

After running the program, it was found that both methods returned true . So why is this? The reasons are as follows:

Generally we believe that basic type variables and object reference variables are stored in the stack, while objects and arrays generated by new are stored in the heap. However, in addition to this, there is an area called the constant pool. Like we usually think of String s1 = "abc"; The value of a string object declared like this is stored in the constant pool.

When we create String s1 = "abc"After such an object, "abc" is stored in the constant pool, when we create a reference String s2 = "abc" , the bottom layer of Java will first search whether "abc" exists in the constant pool. If it exists, let s2 point to this value and will not recreate it. So when comparing using ==, because the addresses are the same, the comparison result is true.

Then the question comes back. Since there is a constant pool, is it true that if the string is not new (generally it is not so new, declaring a string variable is usually written like Example 2), then these strings They all refer to the same object from the constant pool, so just use == for comparison? The answer is yes.

Recommended related articles and tutorials: Getting started with java

The above is the detailed content of How to use if statement to determine whether strings are equal in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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