Java:String uses the difference between equals and == comparison

tags: ==  equals

"==" operator role

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, 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);
        }

The role of equals:

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.


analyse as below:


String is used as an object

Example 1: The object is different, the content is the same, "==" returns false, equals returns true

String s1 = new String("java");
String s2 = new String("java");

System.out.println(s1==s2);            //false
System.out.println(s1.equals(s2));    //true

Example 2: The same object, "==" and equals the same result

String s1 = new String("java");
String s2 = s1;

System.out.println(s1==s2);            //true
System.out.println(s1.equals(s2));    //true

String is used as a basic type

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.

Intelligent Recommendation

00018.02 String object comparison-the difference between == and equals

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...

The difference between equals and equalsIgnoreCase in string comparison

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 difference between == and equals of String in Java

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 difference between == and equals() in java String

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, ...

Difference between equals and == in java (string)

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...

More Recommendation

Java-the difference between equals and == of String

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 ...

The difference between == and equals in Java String

== 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...

The difference between "==" and "equals" in Java String

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...

Difference between equals and == in string in Java

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...

The difference between string equals equals and == in java

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top