search
HomeJavaJavaBaseHow 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

Nov 27, 2019 am 11:55 AM
if statementjavajudgmentstring

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools