Java's String equals method and & quot ;=& quot; comparison, stringequals

Source: Internet
Author: User

Comparison between the equals method of String in java and "=", stringequals

Package stringTest; public class StringDemo {public static void main (String [] args) {// The strings defined in the following two methods are different String s1 = new String ("abc"); String s2 = "abc"; String s3 = "abc "; // s2 and s3 point to the same region in the constant pool/*** equals method in the String class, and rewrite the equals method in the Object. * This method is used to determine whether the String is the same, not the address of the comparison string **/System. out. println (s1.equals (s2); // trueSystem. out. println (s2.equals (s3); // trueSystem. out. println (s1.equals (s3); // true // "=" is the address used to compare string objects. System. out. println (s1 = s2); // falseSystem. out. println (s2 = s3); // trueSystem. out. println (s1 = s3); // false }}

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.