Java detail (the difference between = = and equals)

Source: Internet
Author: User

1) when = = on both sides of the object (String,integer ... ), both sides are more than the address
2) when = = Both sides are basic type (int,float), both sides are value
3) The default equals is the object's address, but the rewrite can be changed to the comparison value, string and integer equals is the rewritten

Talk less, run!!!.

 Packagetest; Public classTest { Public Static voidMain (string[] args) {System.out.println ("############## #对象时比的都是地址 ################"); Testobj T=Newtestobj (); Testobj T2=Newtestobj (); System.out.println (t==T2);//falseSystem.out.println (T.equals (T2));//falseSystem.out.println ("########### #基本类型 [= =] than the value ###########"); intA=1; floatb=1f; System.out.println (A==B);//true//System.out.println (a.equals (b)); wrong spellingSystem.out.println ("######### #Integer特殊 (equals is rewritten as a comparison value, but new = = or compare address) ##########"); Integer I1=0; Integer I2=0; System.out.println (I1==I2);//trueSystem.out.println (I1.equals (I2));//trueInteger i3 =NewInteger (0); Integer I4=NewInteger (0); SYSTEM.OUT.PRINTLN (i3==I4);//falseSystem.out.println (I3.equals (I4));//trueSystem.out.println (I1==I3);//falseSystem.out.println (I1.equals (i3));//trueSystem.out.println (I4.equals (I2));//true        inti5 = 0; System.out.println (I1.equals (i5));//true and basic int type ratio is ratioSystem.out.println (I1==I5);//true and basic int type ratios are also ratiosSystem.out.println (I3.equals (i5));//true new vs. base int type ratioSystem.out.println (I3==I5);//true new and basic int type ratios are also ratios        floatF6 = 0f;//equivalent to f6=0;System.out.println (I1.equals (f6));//false note the result differs in that the override of equals is only the ratio of the int typeSystem.out.println (I1==F6);//trueSystem.out.println (I3.equals (f6));//false note the result differs in that the override of equals is only the ratio of the int typeSystem.out.println (I3==F6);//trueSystem.out.println ("=======string Special ======="); String S1= "a"; String S2= "a"; System.out.println (S1==S2);        System.out.println (s1.equals (S2)); String S3=NewString ("a"); String S4=NewString ("a"); System.out.println (S3==S4);        System.out.println (S3.equals (S4)); System.out.println (S1==S3);        System.out.println (S1.equals (S3));                    System.out.println (s4.equals (S2)); }}

For an integer why only the value of int is compared, see the source code is visible:

/*** Compares this object to the specified object. The result is * {@codetrue if and only if the argument are not * {@codenull} and is an {@codeInteger} object that * contains the same {@codeint} value as this object. *     * @paramobj The object to compare with. * @return  {@codetrue} If the objects is the same; *          {@codefalse} otherwise. */     Public Booleanequals (Object obj) {if(objinstanceofInteger) {            returnValue = =((Integer) obj). Intvalue (); }        return false; }

Java detail (the difference between = = and equals)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.