Java development == and equals()
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

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

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

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

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

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

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.

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