First look and then like, give yourself a little time to think, if it helps you, wechat search [program workplace] pay attention to this persistent workplace programmer. Value: Java skills, interview experience guidance, resume optimization, career planning guidance, skills improvement methods, endless career stories, personal growth experience.

Interviewer: Let me ask about the basics. In development, the most basic, the most overlooked, are the most commonly used skills that must be mastered.

Last time I talked about the interview byte, today is about a question in the interview, in fact, IN the interview byte I found that these big companies attach great importance to some basic details.

So in order to help more people, I will output all of them one by one.

Ok, so let’s start with the comparison of int and integer, and I’m sure most people will look at this and say, well, this is the same thing. Keep reading. I’m sure this article will make sense to you all at once.

1, the concept of

Concepts here are basically what are they?

Int is a basic data type, and the int variable stores values. Integer is a reference type, which is actually an object. Integer stores the address of the reference object.

2. Java primitive data types and their encapsulated classes Because the relationship between int and Integer is the relationship between the base data type and the encapsulated class.

Moving on, what are the basic data types and wrapper classes?

The data type

Byte size

Wrapper class

byte

eight

Byte

short

16

Short

int

32 –

Interger

long

A 64 – bit

Long

float

32 –

Float

double

A 64 – bit

Double

boolean

1 a

Boolean

See this type diagram? The wrapper class for int is Interger.

Java data types have basic data types and reference data types. To facilitate the processing of basic data types as objects, Java introduces the corresponding encapsulation class of basic data types, such as int encapsulation class Integer.

3**, Int and Interger conversion ** The previous conversion of the two is actually a more official name: ** automatic unboxing and automatic boxing

Let’s talk about the concept of unpacking and packing first. **

1, automatic packing automatic packing is to convert the basic data type to the reference data type (object) 2, automatic unpacking is to convert the reference data type to the basic data type

After reading the concept of the basic understanding, is the process of mutual transformation. If you still don’t understand, go down to the code, do an actual search, and fix it.

public static void main(String[] args) { Integer a = 1; // Automatic boxing is used here; Equivalent to Integer a = new Integer(1); int b = a - 1; 1 system.out.println (a); 1 system.out.println (a); System.out.println(b); }Copy the code

4, The difference between Int and Interger ****

Having said that, let me summarize the differences between the two of them, and it will be clearer by the differences

1. Integer is a wrapper class for int, which is a basic Java data type

2. Integer variables must be instantiated before they can be used, whereas int variables do not

3. An Integer is actually a reference to an object. When an Integer is new, it actually generates a pointer to the object. Int stores data values directly

4, the default value of Integer is null, and the default value of int is 0

5, Int and Interger memory comparison ****

Integer objects take up more memory. Integer is an object whose metadata needs to be stored. But int is a primitive type of data, so it takes up less space

6. Comparison of Int and Interger ****

The concept is at least a little fact based, the following through several examples to illustrate one by one, so that you fully understand, well continue to read. Example 1

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false
Copy the code

Analysis: Two variables generated by new are actually references to two Integer objects, so two variables are never equal (because new generates two objects, which are references to objects, with different memory addresses).

Example 2

Integer i = new Integer(100);
int j = 100;
System.out.print(i == j); //true
Copy the code

Analysis: When an Integer variable is compared with an int variable, the result is true as long as the values of the two variables are equal. Because the wrapper class Integer is compared to the base data type int, Java automatically unboxes it to int and then compares it to two int variables

Example 3

Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j); //false
Copy the code

Analysis: Integer variables that are not generated by new are compared to variables generated by new Integer(), resulting in false. Because non-new generated Integer variables point to objects in the Java constant pool, and new Integer() generated variables point to references to newly created objects in the heap, they have different addresses in memory.

Example 4

Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true
Copy the code

Analysis: For two non-new generated Integer objects, the comparison result is true if the values of the two variables are between -128 and 127, and false if the values of the two variables are not within this range.

The above example obviously has a value of 100, between -128 and 127. Let me do one that goes beyond this.

Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false
Copy the code

The result is false.

Java caches numbers between -128 and 127. If Integer I = 127, it caches 127. The next time you write an Integer j = 127, you fetch it directly from the cache.

Me: good interviewer I answer finished, you still have what to say? Interviewer: No, let’s move on to the next question.

Well, today’s article, although short and small, but very clear, easy to understand, can better, faster to learn this knowledge point.

Support me without hesitation forward it !!!!!!

I am [Erdong Shuangyue] a persistent workplace programmer, wechat search [program workplace] attention to me. Don’t forget the triple link, like, like, leave a comment, feel free to give, I don’t choose. Note: If there are any problems with this article, please kindly correct them.