[Java] About same and same value

2 minute read

This time we will talk about the concept of “same” in Java.

Two different meanings of the same word

  • Must refer to the same instance
  • Different instances, but same value

The former is called “equal” and the latter is called “equal”.

About the same

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

At this time, it says that hoge and huga are the same. Equality is determined by the == operator.

For example,

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

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

About equivalency

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. We call such a state that hoge and huga have the same value.

**Equivalence cannot be judged with ==. If you set **hoge==huga, false will be returned.

Equivalence judgment

Use the equals method to determine equivalence. **However, the equals method defined in the Object class is an implementation that checks for equality. **

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

Therefore, whether or not different instances have the same value (equal value) 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;

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

For string string literals that are the same or equivalent

The String type is a reference type, but an instance can be created only with a string literal (enclosed in “”). **Exceptions are instances created with this string literal, where 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 true

This is because the string literal is created as a constant value in the constant memory space different from the instance, and there is a mechanism called “constant pool” for reusing references.

However, this is only when using a string literal, so if you create a variable of the Ring type with the new operator, you cannot use the == operator to determine equivalence.


Thorough capture Java SE11 Silver problem collection