JAVA = = differs from equals

Source: Internet
Author: User

= = is an operator, equals is the method of the string object.

= = Compare values are equal when comparing base data types, and compare addresses when comparing reference objects. Equals () is used only to compare values for equality.

1. Basic data type Comparison

= = You can compare int,double types or string types. Equal to True, otherwise false.

Equals () is a method of a string object and can only be used to compare the value of a string variable. Equal to True, otherwise false.

Example 1:

Code:

public static void Main (string[] args) {TODO auto-generated Method Stubint a=11;int b=11;String c= "ABC";String d= "ABC";if (a==b)System.out.println ("true");ElseSystem.out.println ("false");if (C==d)System.out.println ("true");ElseSystem.out.println ("false");}

Results:

Truetrue

2. Comparison of reference objects

= = compares the address in the stack memory for equality, equal to true otherwise false. Equals compares the value, equal to True, otherwise false.

Example 2:

Code:

public static void Main (string[] args) {String d= "ABC";String E=new string ("abc");if (E==d)System.out.println ("true");ElseSystem.out.println ("false");}

Results:

False

True

JAVA = = differs from 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.