About Java primitive types and reference types

Preface

This is an article that I have compiled in my own way, including the contents of primitives and wrapper classes, and their surroundings. I'm currently studying for a Java Silver qualification, and I'm writing that I understand at that level, so it may lack some rigor or accuracy. Please understand (if you make a mistake, please let me know).

Target audience

・ People who are thinking of getting a Java qualification ・ People who don't understand what they are saying, such as primitives, wrapper classes, and autoboxing.

keyword

Primitive type, reference type, wrapper class, autoboxing, unboxing

What is a primitive (type)?

** [Rough definition] ** ・ One of Java data types -The so-called basic type of Java, also called the value type. -A value is stored in the memory stack area.

[type] There are 8 types in all. I think you don't have to remember the range of values that can be expressed (~~, so I stopped writing in the middle ~~). I think you can remember the number of bits, the larger the number of bits, the wider the range that can be expressed [^ 10].

Data type Values and ranges that can be expressed bit(bit) Concrete example
boolean true, false 1
byte -128 ~127 integers 8
char \u0000 ~ \uFFFF Unicode string 16
short -32768 ~Integer of 32767 16
int integer 32
float Real number(Single precision floating point number) 32 2f, 100F
long integer 64 10l, 5L
double Real number(Double precision floating point number) 64

What is a reference type?

** [Rough definition] ** ・ One of Java data types ・ String, array, List, etc. -The peep area of the memory contains the reference value of the variable, and the stack area contains the value pointed to by the reference value.

If you write the last definition in more detail, the newly created object (≒ instance) will be assigned to the stack area, and the pointer (address in memory) to that object will be assigned to the peep area as a reference value ( Reference 4> From [Primitive types and reference types](https://qiita.com/hysdsk/items/2e94c8722dc8f950e77c# Primitive types and reference types).

[Reference 2] [ref2] is very detailed about the difference in behavior when assigning between primitive type and reference type, so please refer to that. (This time the main subject is not there, so I will move on)

What is a wrapper class?

** [Rough definition] ** ・ Evolutionary class of primitive types classified as reference types ← Looks strong -Each class has a convenient method -It behaves like a primitive type, but the contents may be null (so be careful of nullpo)

It's a wrapper class because it wraps (wraps) primitive types! The contents are primitive types. Let's make a table that corresponds to the primitive type (except for some, it has the same shape as the primitive type!).

Primitive type Wrapper class
boolean Boolean
byte Byte
char Character
short Short
int Integer
float Float
long Long
double Double

I wrote it as a convenient method, but for example, in the case of ʻInteger, convert a string to an int type with ʻInteger.parseInt (String) , convert an int type to a string with ʻInteger.valueOf (int)`, etc. , Various are available (the latter is also used when explicitly converting to Integer type, see the next chapter for details).

What is auto boxing and unboxing?

Based on the explanation so far, if you write the definition

Autoboxing: Automatic conversion from primitive types to wrapper classes Unboxing: Automatic conversion from wrapper class to primitive type [^ 15]

is! I don't think it has anything to do with sports boxing, I don't know [^ 20]! I think it's autoboxing because it automatically wraps the primitive type (= puts it in the box), and conversely it's unboxing because it's taken out of the box.

The actual code looks like this (quoted from Reference 7).

//Auto boxing
int numInt = 10;
Integer numInteger = numInt;

//Unboxing
Integer numInteger = new Integer(10);
int numInt =  numInteger;

The illustration of auto boxing and unboxing looks like this.

Auto boxing
            ==>  
  [Primitive type]        [Wrapper class]  
            <==
Unboxing

Oh, by the way, the fact that I wrote "automatic conversion" above means that you can also explicitly perform autoboxing and unboxing (quoted from Reference 7).

//Auto boxing
int numInt = 10;
Integer numInteger = new Integer(numInt);
//Or below
Integer numInteger = Integer.valueOf(numInt);

//Unboxing
Integer numInteger = new Integer(10);
int numInt =  numInteger.intValue();

In writing

I may or may not write something, it is undecided.

At the end

Thank you for reading to the end!

bonus

** Test information (from Reference 1) ** Subject: Java SE 8 Programmer I Test number: 1Z0-808 [^ 30] Test time: 150 minutes Number of questions: 77 questions Pass line: 65%

reference

In writing this article, I referred to the following books and sites. I would like to take this opportunity to thank you. All references are much easier to understand than my explanations, so I recommend reading them!

  1. Sumito Shiga (2019) "Thorough capture Java SE 8 Silver problem collection [1Z0-808] correspondence" issued by Impress Co., Ltd.
  2. [[Java] Explain the difference between value (primitive) type and reference (object) type with code and diagram << Lecture Day 1 >>] [ref2]
  3. [Primitive type [Variable type]] [ref3](IT terminology dictionary that makes you feel like "I understand" even if you "I don't understand")
  4. [Java reference understood in the figure] [ref4]
  5. [[Introduction to Java] An easy-to-understand explanation of the basics of Integer and wrapper classes! ] [ref5]
  6. [Wrapper class type] [ref6]
  7. [[Java] Auto Boxing, Unboxing] [ref7]

[]: URL list [ref2]: https://freelance-jak.com/technology/java/1175/ [ref3]: https://wa3.i-3-i.info/word15876.html [ref4]: https://qiita.com/hysdsk/items/2e94c8722dc8f950e77c [ref5]: https://www.sejuku.net/blog/22828 [ref6]: https://www.javadrive.jp/start/wrapper_class/index1.html [ref7]: https://qiita.com/chihiro/items/870eca6e911fa5cd8e58

Recommended Posts

About Java primitive types and reference types
About Java data types (especially primitive types) and literals
Basic data types and reference types (Java)
Java primitive types, reference types, Immutable, Mutable
About Java basic data types and reference type memory
Java basic data types and reference types
[Java] Variables and types
About Java class loader types
I investigated Java primitive types
[Java] About String and StringBuilder
About Java Packages and imports
Equivalence comparison of Java wrapper classes and primitive types
About Java static and non-static methods
Java variable declaration, initialization, and types
[Java] Basic types and instruction notes
Java Primer Series (Variables and Types)
[Java beginner] About abstraction and interface
Java reference mechanism (stack and heap)
Java array variables are reference types
Java pass by value and pass by reference
Difference between primitive type and reference type
This and that about Base64 (Java)
[Java] Exception types and basic processing
[About JDBC that connects Java and SQL]
[Introduction to Java] Variable declarations and types
Page number logic and reference code (java)
About Java interface
[Java] About Java 12 features
[Java] About arrays
Java8 method reference
Something about java
Where about java
About Java features
About Java threads
Java and JavaScript
About Java arrays
XXE and Java
About interface, java interface
java8 method reference
JAVA reference materials
My Java reference
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()
Getters and setters (Java)
About Java StringBuilder class
[Java] Types of comments and how to write them
[Java] Thread and Runnable
Java true and false
[Java] About Singleton Class
About Bean and DI
A memo about the types of Java O/R mappers and how to select them
About classes and instances
[Java] String comparison and && and ||
About Java method binding