[JAVA] Difference between == operator and eqals method

Domo is Fugito.

This time is a little memorandum.

What is the difference between the == operator and the equals method?

in conclusion,

"** == operator determines identity and The equals method determines identity and equivalence ** "

There seems to be a difference.

What is "identity judgment"?

"Whether or not they are the same instance".

What is "equivalence judgment"?

"The values stored in the instance are the same Whether or not it is. "

An example is shown below.

Sample program

public class Example{
    public static void main(String[] args){
        String s1 = "012";
        String s2 = new StringBuilder("012").toString();

        if(s1 == s2){
            System.out.println("true");
        }else{
            System.out.println("false");
        }

        if(s1.equals(s2)){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
    }
}

"s1 == s2" means "** whether s1 and s2 are the same instance **" It will be judged. In the above program, s1 and s2 are independent of each other. Created as an instance. So the same instance I can't say that, so ** false ** is returned. By the way, as follows When rewritten, s1 == s2 becomes true.

String s1 = "012";
String s2 = s1;

On the other hand, the equals method is the same in the method by the == operator first. After determining the sex, the equivalence is determined. Here, ** for s1 and s2 Since the same string literal "012" is stored **, s1.equals (s2) Is ** true **.

Summary

-The == operator determines ** identity ** -The equals method determines ** identity and equivalence ** -Identity judgment is "** Is it the same instance " -Judgment of equivalence is " stored in the instance Whether the values are the same ** "

Recommended Posts

Difference between == operator and eqals method
Difference between == operator and equals method
Difference between render method and redirect_to
Difference between vh and%
Difference between i ++ and ++ i
[Rails] Difference between create method and new + save method
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
Ethereum Transaction Sending Method Difference between send and sendAsync
[Ruby] Difference between get and post
Difference between interface and abstract class
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[Java beginner] == operator and equals method
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
What is the difference between an action and an instance method?
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
Let's override the difference between == (identity) and equals method (equivalence)
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Easy to understand the difference between Ruby instance method and class method.
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList