The equals () method in the String class, stringequals

Source: Internet
Author: User

The equals () method in the String class, stringequals

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

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 three String variables, two of which are assigned with a constant expression "Programming", and the other with a new value of the String class "Programming. Comparison Using the equals (...) method and the "=" operator produces the following results:
S0. Equals (s1): true
S0. Equals (s2): true
S0 = s1: false
S0 = s2: true

String. the equals () method compares the content of a string and uses equals (...) the method compares all characters in the string one by one. If they are completely equal, true is returned. In this case, all strings are the same, so when the string s0 is compared with s1 or s2, the returned value is true. The operator "=" compares the String instance references.

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.