[Java] Judgment of identity and equivalence

Judgment of Java identity and equivalence

I summarized about.

[What is the same / equivalent value]

Java has two meanings in the word "same"

--"** Same" that they are the same instance ** --The same value is "** equivalent" **

It's called.

[What is identity / equivalence]

-** Instances are different, but equivalence that they have the same value ** -** Identity that multiple variables refer to the same instance **

It's called.

Each judgment method is as follows.

-** Identity is == operator ** -** equals method ** to see if the referenced instances have the same value **

[Sample code]

The code below is like this.

  1. Create one Sample instance with the main method
  2. Prepare another Sample type variable and assign a reference to the first instance
  3. Create an instance of Sample and assign a reference to that instance to sample1.

I wrote the detailed processing flow in the comment out, but in the end sample1 and sample2 will be different instances, so the result will be "false".

public class Sample {
    private int num;

    public Sample(int num) {
        this.setNum(num);
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
public class Main {

    public static void main(String[] args) {

        // 1.Create one instance of Sample
        Sample sample1 = new Sample(10);

        // 2.Prepare another Sample type variable and assign a reference to the first instance
        Sample sample2 = sample1; // sample1,sample2 is "same" * Must be the same instance

        // 3.In addition, create an instance of Sample and assign a reference to that instance to sample1.
        sample1 = new Sample(10);//sample1 and sample2 are not "identical" because they have references to different instances

        System.out.println(sample1 == sample2); //Result false

    }

}
false

[Summary]

As a measure against Silver ...

-** Instances are different, but having the same value is called equivalence, and it is judged by the equals method. ** ** -** Identity is the property that multiple variables refer to the same instance, and is judged by the == operator. ** **

It's confusing, but remember that if you create a new instance with the same name, it will be a different instance.

References / Articles

-[Thorough capture Java SE 11 Silver problem collection [1Z0-815] correspondence](https://www.amazon.co.jp/%E5%BE%B9%E5%BA%95%E6%94%BB%E7 % 95% A5Java-SE-11-Silver% E5% 95% 8F% E9% A1% 8C% E9% 9B% 86-1Z0-815/dp/4295007625/ref = pd_lpo_14_img_0/356-5238196-1895250? _Encoding = UTF8 & pd_rd_i = 4295007625 & pd_rd_r = 1c21865b-a9fe-43e8-ac01-46170e44abca & pd_rd_w = 9El0Q & pd_rd_wg = hh5Hc & pf_rd_p = 4b55d259-ebf0-4306-905a-7762d1b93740 & pf_rd_r = 72AVCH

Recommended Posts

[Java] Judgment of identity and equivalence
Judgment of JSONArray and JSONObject
Advantages and disadvantages of Java
Equivalence comparison of Java wrapper classes and primitive types
About fastqc of Biocontainers and Java
Identity / equivalence
After 3 months of Java and Spring training
[Java] Inheritance and structure of HttpServlet class
[Java / Swift] Comparison of Java Interface and Swift Protocol
Equal value judgment of Integer and int
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above
This and that of the implementation of date judgment within the period in Java
Java and JavaScript
XXE and Java
[Java] Overview of Java
[Java] Personal summary of classes and methods (basic)
[Java] The confusing part of String and StringBuilder
JAVA: Realizes generation and scanning of various barcodes
Basics of threads and Callable in Java [Beginner]
[Java] Classification memo of compilation error and run-time error
Java enables extraction of PDF text and images
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
Getters and setters (Java)
[Java] Types of comments and how to write them
[Java] Thread and Runnable
Summary of ToString behavior with Java and Groovy annotations
The comparison of enums is ==, and equals is good [Java]
NIO.2 review of java
Review of java Shilber
Java true and false
[Java] String comparison and && and ||
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
java --Unification of comments
Judgment of the calendar
[Java] Holiday judgment sample
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
History of Java annotation
java (merits of polymorphism)
[Java] About Objects.equals () and Review of String comparisons (== and equals)
Java --Serialization and Deserialization
[Java] Arguments and parameters
I summarized the types and basics of Java exceptions
timedatectl and Java TimeZone
List of frequently used Java instructions (for beginners and beginners)
[Java] Branch and repeat
Use of Abstract Class and Interface properly in Java
NIO review of java
[Java] [Kotlin] Generically call valueOf and values of Enum
[Java] Variables and types
[Java10] Be careful of using var and generics together
[Java] Three features of Java
java (classes and instances)
Summary of Java support 2018
[Java] Handling of character strings (String class and StringBuilder class)
[Java] Overload and override