Home Java javaTutorial Java development == and equals()

Java development == and equals()

Jul 23, 2018 am 09:48 AM
java java development

The

== sign compares values ​​when comparing basic data types, but when using the == sign to compare two objects, it compares the address values ​​of the two objects.

equals() In the case of overriding, the memory address is compared, but most classes in Java have overridden the equals() method, so the values ​​are compared

String str1 = "abc";
String str2 = "abc";
System.out.println(str1.equals(str2));
System.out.println(str1 == str2);

In this case, true and true are returned, and the second = = also returns true:

Because the constant pool in the memory belongs to the method area. When str1 is created, there is no constant pool, so the object "abc" is created in the constant pool. When str2 is created, the constant It already exists in the pool, so it will be used directly when creating it for the second time, so the address is the same

If it is changed to

String str1 = new String("abc");
String str2 = new String("abc");

str1==str2, it will return false because two objects are created. , the address is different.

The above is the detailed content of Java development == and equals(). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1510
276
Fixed: Windows Update Failed to Install Fixed: Windows Update Failed to Install Aug 08, 2025 pm 04:16 PM

RuntheWindowsUpdateTroubleshooterviaSettings>Update&Security>Troubleshoottoautomaticallyfixcommonissues.2.ResetWindowsUpdatecomponentsbystoppingrelatedservices,renamingtheSoftwareDistributionandCatroot2folders,thenrestartingtheservicestocle

How to use a while loop in Java How to use a while loop in Java Aug 08, 2025 pm 04:04 PM

AwhileloopinJavarepeatedlyexecutescodeaslongastheconditionistrue;2.Initializeacontrolvariablebeforetheloop;3.Definetheloopconditionusingabooleanexpression;4.Updatethecontrolvariableinsidethelooptopreventinfinitelooping;5.Useexampleslikeprintingnumber

What is the process of serialization for a Java object? What is the process of serialization for a Java object? Aug 08, 2025 pm 04:03 PM

Javaserializationconvertsanobject'sstateintoabytestreamforstorageortransmission,anddeserializationreconstructstheobjectfromthatstream.1.Toenableserialization,aclassmustimplementtheSerializableinterface.2.UseObjectOutputStreamtoserializeanobject,savin

What is a HashMap in Java? What is a HashMap in Java? Aug 11, 2025 pm 07:24 PM

AHashMapinJavaisadatastructurethatstoreskey-valuepairsforefficientretrieval,insertion,anddeletion.Itusesthekey’shashCode()methodtodeterminestoragelocationandallowsaverageO(1)timecomplexityforget()andput()operations.Itisunordered,permitsonenullkeyandm

How to create and use an array in Java How to create and use an array in Java Aug 11, 2025 pm 04:00 PM

TocreateanduseanarrayinJava,firstdeclarethearraywiththedatatypeandsquarebrackets,theninstantiateitwiththenewkeywordorinitializeitdirectlywithvalues;1.DeclareandcreateanarrayusingdataType[]arrayName=newdataType[size];or2.InitializedirectlywithdataType

How do you create a thread in Java? How do you create a thread in Java? Aug 11, 2025 pm 01:34 PM

YoucancreateathreadinJavabyextendingtheThreadclassorimplementingtheRunnableinterface.2.ExtendingThreadinvolvescreatingaclassthatoverridestherun()methodandcallingstart()onaninstance.3.ImplementingRunnablerequiresdefiningtherun()methodinaclassthatimple

python argparse required argument example python argparse required argument example Aug 11, 2025 pm 09:42 PM

When using the argparse module, the parameters that must be provided can be achieved by setting required=True. 1. Use required=True to set optional parameters (such as --input) to be required. If not provided when running the script, an error will be reported; 2. Position parameters are required by default, and there is no need to set required=True; 3. It is recommended to use position parameters for necessary parameters. Occasionally, the optional parameters of required=True are used to maintain flexibility; 4. required=True is the most direct way to control parameters. After use, the user must provide corresponding parameters when calling the script, otherwise the program will prompt an error and exit.

How to use a Set in Java How to use a Set in Java Aug 11, 2025 am 11:57 AM

ChoosetheappropriateSetimplementation:useHashSetforfastoperationswithoutorder,LinkedHashSetforinsertionorder,andTreeSetforsortedorder.2.Addelementswithadd()andremovewithremove(),whereadd()returnsfalseiftheelementisalreadypresent.3.Checkforelementsusi

See all articles