Comparison of objects in java --- = precautions for using equals

Source: Internet
Author: User

Code on

Public class JavaTest1 {public static void main (String [] agrs) {int a = 100; int B = 100; int c = B; string str1 = new String ("java"); String str2 = new String ("java"); String str3 = str2; if (a = B) {System. out. println ("a = B");} else {System. out. println ("! = B ");} if (B = c) {System. out. println (" B = c ");} else {System. out. println (" B! = C ");} if (str1 = str2) {System. out. println (" str1 = str2 ");} else {System. out. println (" str1! = Str2 ");} if (str2 = str3) {System. out. println (" str2 = str3 ");} else {System. out. println (" str2! = Str3 ");} if (str1.equals (str2) {System. out. println ("str1 equals str2");} else {System. out. println ("str1 doesn't equal str2");} if (str2.equals (str3) {System. out. println ("str2 equals str3");} else {System. out. println ("str2 doesn't equal str3 ");}}}


Analysis: There are two methods for comparing objects. They are the "=" operator and the equals () method, the "=" operator is used to compare whether the memory address values of two objects are equal. The equals () method is used to compare whether the content of the two objects is consistent.

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.