The Equals () method in the String class

Source: Internet
Author: User

In Java, each object has an address space, in which the value of the object is preserved. Equals compares the value, = = compares the address and the value.

01:public class Stringexample
02: {
03:public static void Main (String args[])
04: {
05:string S0 = "Programming";
06:string S1 = new String ("Programming");
07:string s2 = "program" + "Ming";
08:
09:system.out.println ("S0.equals (S1):" + (S0.equals (S1)));
10:system.out.println ("S0.equals (S2):" + (S0.equals (s2)));
11:system.out.println ("S0 = = S1:" + (S0 = = S1));
12:system.out.println ("S0 = = S2:" + (S0 = = s2));
13:}}
This example contains 3 string variables, of which two are assigned the constant expression "programming" and the other is an instance of the string class that is assigned a new value of "programming". Use Equals (...) The comparison method and the "= =" operator produce the following results:
S0. Equals (S1): True
S0. Equals (S2): True
S0 = = S1:false
S0 = = S2:true

The String.Equals () method compares the contents of a string, using equals (...). Method compares all characters in a string one by one, and returns true if it is exactly equal. In this case all strings are the same, so the return value we get when the string s0 is compared to S1 or S2 is true. the operator "= =" compares a reference to a string instance.

The Equals () method in the String class

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.