[Meow & quot; road to Android] [Fan Wai] About = and equals, androidequals

Source: Internet
Author: User

[Meow "road to Android] [Fan Wai] About = and equals, androidequals
[Meow "'s Android path] [outside] = and equals often use = and equals to determine whether the variables are the same in actual programming. However, these two comparison methods are often confusing in the cloud. The following is my personal summary, hoping to play a role in setting the cloud to see the day. [Pre-lecture popularity] Please read [meow "'s Android path] [BASICS (1)] [Java object-oriented Basics] data types and operators understand basic Java data types and Reference Data Types 1. "=" operator Introduction, = The operator compares whether the content of two variables in the stack is the same. Take a = B as an example: 1.1 If a and B are all basic data types

  • A and B are incompatible and cannot be compared
  • A and B are compatible and compare the values in the stack
//Code:int a = 1000;int b = 1000; //Result:a == b : true

 

1.2 if a is the basic data type and B is the reference type
  • A and B are incompatible and cannot be compared
  • If a and B are compatible, the content of a and B in the stack is compared.
1 //Code:2 int a = 1000;3 Integer b = 1000;4 Integer c = new Integer(1000);5  6 //Result:7 a == b:true8 a == c:true

 

1.3 If a and B are of the reference type
  • A and B are incompatible and cannot be compared
  • If a and B are compatible, the addresses of objects a and B stored in the stack are compared (null is not considered)
1 // simply put, compare whether a and B point to the same instance (memory block ). 2 Code: 3 String a = "Test"; 4 String B = "Test"; 5 String c = new String ("Test "); 6 String d = new String ("Test"); 7 8 Integer x = 1000; 9 Integer y = new Integer (1000); 10 Integer z = new Integer (1000 ); 11 12 Result: 13 a = B: true14 a = c: false15 c = d: false16 17 x = y: false18 y = z: false19 // In the above example, "Test" is placed in the constant pool, and the addresses of objects a and B all point to this constant. C and d are re-opened in the heap to store the "Test" separately, so the memory addresses pointed to by c and d are also different.

 

2. The equals method removes the custom equals method, and some classes have special implementations for the equals method. In general, equals compares whether the substantive content of the two objects is the same. Take B. equals (a) as an example: 2.1 If a and B are both basic data types, it cannot be compared. 2.2 if a is basic data type, B is reference type
  • If a and B are incompatible, the result must be false.
  • If a and B are compatible, compare whether the value of a in the stack is the same as that of B in the heap memory.
1 //Code:2 int a = 1000;3 Integer b = 1000;4 Integer c = new Integer(1000);5  6 //Result:7 b.equals(a):true8 c.equals(a):true

 

2.3 If a and B are of the reference type
  • If a and B are not the same instances, the result must be false.
1 //Code:2 Object a = new String("Test");3 Object b = new StringBuilder("Test");4 5 //Result:6 b.equals(a):false
  • If a and B are the same instances, compare whether the values of a and B in heap memory are the same
//Code:String a = "Test";String b = "Test";String c = new String("Test");String d = new String("Test"); Integer x = 1000;Integer y = new Integer(1000);Integer z = new Integer(1000); //Result:b.equals(a):truec.equals(a):truec.equals(d):true y.equals(x):truey.equals(z):true

 

This article is original from Nodin. For more information, see the source! Http://www.cnblogs.com/monodin/p/3841219.html


Android statements are moderate to equals, not equal?

Add an exclamation point to the brackets of the if statement.
For example:
If (a. equals (B) indicates whether a is equal to B.
If (! A. equals (B) indicates determining whether a is not equal to B.

Usage of TextUtilsisEmpty () and equals () methods in android

Carefully read the official API: Returns true if the string is null or 0-length. Because you return a variable from EditText. If the variable itself is null, an error is returned if you drop its equals method. However, if you call TextUtils. isEmpty () to pass this variable as a parameter. If this parameter is null or "", the system returns true. Therefore, the official version is more rigorous. And. It is also very convenient. This is because you have to write an if statement to determine whether to use the statement. The returned value is a boolean value. Why can't someone else leave the road?

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.