search
HomeJavaJavagetting StartedThe difference between java equals and ==

The difference between java equals and ==

java9 example code: (Recommended learning: java course)

String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
String str4 = new String("abc");
当:  str1 == str2    输出:true    当:str1.equals(str2); 输出:true
当:  str1 == str3 输出:false      当:str1.equals(str3); 输出:true
当:  str3 == str4 输出:false      当:str3.equals(str4); 输出:true

Details involved:

- You can use the intern method in String. The string object has a reference to the string that is equal to it in the constant pool.

str3.intern() == str4.intern()  输出:true
str1.intern().equals(str2.intern())  输出:true
str1.intern() == str1   输出:false
- String str = new String("abc");创建了几个对象?

First of all, check whether there is "abc" in the constant pool. This string, if it exists (String str = "abc"; when it appears), creates one, if not, creates two (one in the constant pool, one in the heap).

The difference between equals and ==

For ==:

Acts on variables of basic data types , then directly compare whether the stored "values" are equal;

acts on reference type variables, then the comparison is the address of the object pointed to;

For equals:

The equals method cannot be applied to variables of basic data types;

If there is no equals method in Object If rewritten, the comparison is the address of the object pointed to by the reference type variable, otherwise the content is compared

The above is the detailed content of The difference between java equals and ==. 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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)