Equals location:
In the Object class, and Object is the parent class of all classes, included in jdk, but not suitable for most scenarios, usually need to be rewritten
public boolean equals(Object obj) {
return (this == obj);
}
Used to determine whether two variables are references to the same object, that is, whether the contents of the heap are the same, and the return value is a Boolean type.
Basic use of equals:
boolean b = obj1.equals(obj2);
The String type compares whether the contents of different objects are the same. You should use equals because == has different functions for comparing reference types and comparing basic data types.
String s1 = new String("java");
String s2 = new String("java");
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true
String s1 = new String("java");
String s2 = s1;
System.out.println(s1==s2); //true
System.out.println(s1.equals(s2)); //true
If the values are not the same, the objects are not the same, so "==" is the same as the equals result
String s1 = "java";
String s2 = "java";
System.out.println(s1==s2); //true
System.out.println(s1.equals(s2)); //true
If there is no String object in the String buffer pool with the same value as the specified value, then the virtual machine will create a new String object for this purpose and store it in the String buffer pool.
If the String buffer pool has a String object with the same value as the specified value, then the virtual machine will not create a new String object for this purpose, but directly returns a reference to the existing String object.
Series Article Directory Article Directory Series Article Directory Preface 1. ==, compare object addresses Two, equals comparison, compare the content of the string Three, equalsIgnoreCase(String ano...
Today I saw in the code that my colleagues used it when comparing the difference: equals is case-sensitiveequalsIgnoreCase is not case sensitive Use specific methods for specific scenarios...
The printed result is: Attention The print result is false. Because abc and abc2 for some reasons of system optimization, although two "abc" are defined, the system does not all...
The role of the "==" operator: 1, for comparison of basic data types 2. Determine if the reference points to the same block address of the heap memory. Equals location: In the Object class, ...
Without further ado, first give the source code of the equals method in String analysis 1. As you can see in the code, the first step in the method is to judge yes ==, if it is satisfied, it returns d...
Post code first operation result: s1: abcdef s2: abcdef s3: abcdef ===equals() succeed=== ===s3==s1=== ===s3equals()s1=== ===s2equals()s3=== Because of the existence of the constant pool. The runtime ...
== Basic data type comparison value There are 8 basic data types in Java. The values of basic data types are not stored in the heap. Basic data types use ==The comparison is the size of the value. S...
For Java, String's equals () method is compared to the content of the string, == Compare the memory address value. E.g: } The output result is: Output result It can be seen that the equals () method i...
simply put 1. == is whether the address corresponding to the two objects is equal, 2. Equals is compared whether the expression of two string is equal First of all, we must understand that when we get...
Equals is the value of the comparison, and == the address of the comparison, as for why a will be the same as the address of b, this involves the problem of the string constant pool, you can read a bl...