About the same and equivalent

This time we are talking about the concept of "same" in Java.

Two different meanings of the word same

--Refering to the same instance --The instances are different, but the values are the same

The former is called "equal" and the latter is called "equivalent".

About the same

Object hoge = new Object();
Object huga = hoge; //Copy the hoge reference and assign it to huga

At this time, hoge and huga are said to be the same. The identity is judged by the == operator.

For example

Object hoge = new Object();
Object huga = new Object();
huga = hoge;

Even so, hoge and huga are not the same (because they are different instances).

About equivalence

public class Cat{
  private String name;
  public Cat(String name) {
    this.name = name;
  }
}

Create two instances with the same name value using the class.

    Cat hoge = new Cat("neko");
    Cat huga = new Cat("neko");

As mentioned in the previous section, these two instances are not the same because they have different references. However, each reference has the same value. Such a state is said to be equivalent between hoge and huga.

** Equivalence cannot be judged with ==. ** hoge == huga returns false.

Judgment of equivalence

Use the equals method to determine equivalence. ** However, the equals method defined in the Object class is implemented to check the identity. ** **

public boolean equals(Object obj){
  return (this == obj);
}

Therefore, whether different instances have the same value (equivalence) is determined by overriding the ** equal method. ** **

public static class Cat {
    private String name;
    public Cat(String name) {
      this.name = name;
    }
    public boolean equals(Object obj) {
      if (obj == null){
        return false;
      }
      if (obj instanceof Cat){
        Cat cat = (Cat) obj;
        return cat.name == this.name;
      }
      return false;
    }
  }

If so, you can judge equivalence by using hoge.equals (huga). By the way, depending on how you write the override of the equal method, you can also judge that in a class with multiple values, if one of the values matches, it is considered to be the same value.

About the same and equivalent string literals

The String type is a reference type data type, but an instance can be created only by a string literal (enclosed in ""). ** Instances created with this string literal are an exception, and the == operator can be used to determine equivalence. ** **

public static void main(String[] args) {
    String a= "neko";
    String b = "neko";
    System.out.println(a == b); //Returns with true
  }

This is because string literals are created as constant values in a memory space for constants different from the instance, and there is a mechanism called "constant pool" for reusing references.

However, since this is only when a string literal is used, if a Sring type variable is created with the new operator, the equivalence cannot be determined with the == operator.

References

Thorough capture Java SE11 Silver problem collection

Recommended Posts

About the same and equivalent
About the equals () and hashcode () methods
About the operation of next () and nextLine ()
About the method
About the difference between irb and pry
About the package
About the mechanism of the Web and HTTP
Think about the combination of Servlet and Ajax
[Rails / ActiveRecord] About the difference between create and create!
About next () and nextLine () of the Scanner class
A note about the Rails and Vue process
Output about the method # 2
About the StringBuilder class
Commentary: About the interface
About the asset pipeline
About Bean and DI
About classes and instances
About the ternary operator
About gets and gets.chomp
About redirect and forward
About the Kernel module
About encapsulation and inheritance
About the authenticate method.
About the map method
About Serializable and serialVersionUID
About the ancestors method
[Output] About the database
About the [ruby] operator
About the to_s method.
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
About the difference between classes and instances in Ruby
About the relationship between HTTP methods, actions and CRUD
About Class loading and initialization when the JVM starts
[Technical memo] About the advantages and disadvantages of Ruby
About the handling of Null
About specifying the JAXRS path
About for statement and if statement
About synchronized and Reentrant Lock
Output about the method Part 1
about the where method (rails)
A note about the scope
About the description of Docker-compose.yml
About Ruby hashes and symbols
[Java] About String and StringBuilder
About the Android life cycle
[Ruby] About the difference between 2 dots and 3 dots of range object.
About classes and instances (evolution)
About pluck and ids methods
About the explanation about functional type
Consideration about classes and instances
Think about the differences between functions and methods (in Java)
About Java Packages and imports
About Ruby and object model
About the programming language Crystal
How to run React and Rails on the same server
Consideration about the times method
About Ruby classes and instances
About instance variables and attr_ *
About self-introduction and common errors
[Java] I thought about the merits and uses of "interface"