Java easy to mix small knowledge--equals method and = = Difference

Source: Internet
Author: User

The difference between equals method and = =

1.equals is a method of a string object and can be passed "." Call.

2.== is an operator.

Second, the common use of comparative

1, the basic data type comparison.

equals and = = Both compare two values for equality. Equality is true, and inequality is false.

2. Comparison of reference objects.

equals and = = Both compare the address in the stack memory for equality. Equality is true, and inequality is false.

Three, easy wrong point

1, string is a special type of reference. For a comparison of two strings, either = = or equals, the strings are the same.

2. When you create two string objects, the addresses in memory are not the same, and you can assign the same values.

   So the contents of the string are the same. Referenced addresses are not necessarily the same.

3, the basic data type comparison (except string) = = and equals are comparison values.

Iv. Examples of demonstrations

Comparing String objects: Ex

  

 Public class testequals {        publicstaticvoid  main (String args[]) {        new String ("Hello");         New String ("Hello");        System.out.println ("S1 = = S2 The result is:" + (S1 = = s2)); // false        The result of System.out.println ("S1.equals (S2)" is: "+s1.equals (S2)); // true     }}

The base data type is omitted here (the effect is the same).

Java easy to mix small knowledge--equals method and = = Difference

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.