preface

The fundamental difference between Integer and int is that Integer is a wrapper class and int is a primitive data type (nonsense).

This article is intended to illustrate the difference between Integer and int in more detail and give examples

Integer and int

The default initial value of an Integer is null, and the initial value of an int is int, which means that an Integer can tell the difference between an unassigned value and a value of 0, whereas an int can’t tell the difference between an unassigned value and a value of 0. Therefore, int is not suitable for filling in form data at the Web layer. (for example, if a student takes an exam and scores 0, and if a student doesn’t take an exam, scores are null.) Integer is a class with a number of built-in methods, while int can only do some basic ±*/= operations. Integer variables must be instantiated first

About the Integer

There are a few things to mention when describing Integer:

Unpacking and packing

When to unbox: Base datatypes and reference datatypes for operations when to box: Base datatypes for assigning “==” and “equals()” to reference datatypes

“==” : compares values when comparing base datatype, compares object heap memory address equals() when comparing reference datatype: In the Object class, this is done by “==”, but most of the time, we override this in the Integer class, where “==” is used to compare whether objects have the same address, and equals() is overridden, which checks whether the objects in the argument are of type Integer, and if so, whether they have the same value.

 public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

Copy the code

Some notes

When an Integer variable is compared with an int variable, the result is true as long as the values are equal.

Integer a = new Integer(1); int b = 1; System.out.println(a == b); //trueCopy the code

Two Integer variables generated by new are never equal (because new generates two new objects with different memory addresses)

Integer a = new Integer(1); Integer b = new Integer(1); System.out.println(a == b); //falseCopy the code

A variable created by new is not equal to an Integer variable created by non-new (because the Integer variable created by non-new refers to an object in the Java constant pool, while the Integer variable created by new refers to an object created by the heap).

Integer a = new Integer(1); Integer b = 1; System.out.println(a == b); //falseCopy the code

Comparison of two non-new Integer variables: true when the value is between -128 and 127, false when the value is outside this range (because the Integer constant pool is stored in the range [-128,127], where the value is stored directly in the constant pool, both pointing to the same address). Numbers outside this range require the creation of a new object through heap memory. For example, Integer a=1 will write 1 into the cache, and the next Integer b=1 will be written directly out of the cache, so the address will be the same.

Integer a = 1; Integer b = 1; System.out.println(a == b); //true Integer a = 129; Integer b = 129; System.out.println(a == b); //falseCopy the code

The last

Welcome to pay attention to the public number: the future has light, receive a line of large factory Java interview questions summary + the knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary! These are some of the things that the interviewer should ask during the interview. These include basics, Java collections, JVMS, multi-threaded concurrency, Spring principles, microservices, Netty and RPC, Kafka, diaries, design patterns, Java algorithms, databases, Zookeeper, distributed caching, data structures, and more.