Let's take a look at such a topic first. Suppose there is the following code. The statement that returns false in the following options is?
String s = "hello"; String t = “hello”; char c [ ] = {'h','e','l','l','o'}; s.equals (t); //A t.equals (c); //B s==t; //C t.equals (new String ("hello")); //D
Before publishing the answer, let's take a few knowledge points.
1. Java data types are divided into:
- basic data type
- reference data type
2. For "=="
- Used to compare basic data types with each other. Compare the two values for equality.
- Used to compare reference data types with each other. Compare whether the two addresses are equal.
- Cannot be used for primitive and reference comparisons.
3. For "equals"
- Cannot be used for primitive data type comparison (because this is a method, inherited from object).
- It is used to compare objects, and compare whether the reference addresses of the two are the same.
4. Special circumstances
- Numeric primitives and numeric classes have autoboxing and auto-unboxing .
- Strings exist as constants, and if multiple string variables have the same value, they point to the same address.
- There will be automatic type conversion for numeric types .
At this point, the answer is very clear, choose B.
Author: Xinxin Xin, published in Blog Park
Please indicate the source when reprinting. Email communication is welcome: [email protected]