Equivalence comparison of Java wrapper classes and primitive types

In a certain project, the equivalence comparison between the wrapper class and the primitive type was wrong, so I wrote an article for newcomers.

DemoApp.java


public class DemoApp {
    
    public static void main(String[] args) {
        Integer x = 999999;
        Integer y = 999999;
        int z = 999999;

        //Of the wrapper class==Comparison
        if (x == y) {
            System.out.println("same");
        } else {
            System.out.println("other");
        }

        //Comparison of wrapper classes by equals method
        if (x.equals(y)) {
            System.out.println("same");
        } else {
            System.out.println("other");
        }

        //Wrapper classes and primitive types==Comparison
        if (x == z) {
            System.out.println("same");
        } else {
            System.out.println("other");
        }
        
        //Comparison of wrapper class and primitive type equals method
        if (x.equals(z)) {
            System.out.println("same");
        } else {
            System.out.println("other");
        }
    }
}

Execution result


other
same
same
same

This is the result of defining a wrapper class with the same number and a variable of primitive type and comparing the values.

Item number conditions result
1 Wrapper class and wrapper class==Equivalence comparison different
2 Wrapper class and wrapper classequalsEquivalence comparison by method the same
3 Wrapper class and primitive type==Equivalence comparison the same
4 Wrapper class and primitive typeequalsEquivalence comparison by method the same

== determines whether it points to the same instance (object), but it is interesting that the wrapper class and primitive type are also determined to be ** the same **.

Recommended Posts

Equivalence comparison of Java wrapper classes and primitive types
[Java] Judgment of identity and equivalence
About Java primitive types and reference types
Comparison of JavaScript objects and Ruby classes
[Java] Personal summary of classes and methods (basic)
About Java data types (especially primitive types) and literals
[Java] String comparison and && and ||
[Java] Variables and types
java (classes and instances)
[Java] Types of comments and how to write them
The comparison of enums is ==, and equals is good [Java]
I summarized the types and basics of Java exceptions
I investigated Java primitive types
Advantages and disadvantages of Java
Java abstract methods and classes
[For beginners] Explanation of classes, instances, and statics in Java
[Java] Comparison method of character strings and comparison method using regular expressions
Generate Stream from an array of primitive types in Java
[Java] Speed comparison of string concatenation
Comparison of WEB application development with Rails and Java Servlet + JSP
[Java] Various summaries attached to the heads of classes and members
Java variable declaration, initialization, and types
About fastqc of Biocontainers and Java
Java Primer Series (Variables and Types)
Basic data types and reference types (Java)
Java generics (defines classes and methods)
Comparison of processing times based on awk, shell command, and Java
[Java] Correct comparison of String type
Java basic data types and reference types
List of types added in Java 9
Classes and instances Java for beginners
[Java] Exception types and basic processing
Comparison of thread implementation methods in Java and lambda expression description method
Comparison of nullable, non-nullable, non-nullable and safe types from the perspective of null safety
From Java9, the constructor of the class corresponding to primitive types is deprecated.
Java starting from beginner, variables and types
Regarding String type equivalence comparison in Java
Comparison of Android Handler Looper and runOnUiThread
After 3 months of Java and Spring training
[Java] Inheritance and structure of HttpServlet class
Java programming (classes and instances, main methods)
Thorough comparison of Android and iOS implementations
JAVA learning history abstract classes and methods
[Java] Comparison of String type character strings
[Introduction to Java] Variable declarations and types
Summary of Java Math.random and import (Calendar)
[Java ~ Classes and External Libraries ~] Study Memo (6)
[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
The story of low-level string comparison in Java
About the idea of anonymous classes in Java
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
Chrome59 Comparison of normal and headless mode operation
I compared the characteristics of Java and .NET
Differences in writing Java, C # and Javascript classes
JAVA: Realizes generation and scanning of various barcodes
Kantai Collection Java # 1 Classes and Objects [For Beginners]
Basics of threads and Callable in Java [Beginner]
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
Java enables extraction of PDF text and images