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).
・ 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.
Primitive type, reference type, wrapper class, autoboxing, unboxing
** [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 |
** [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)
** [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).
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();
I may or may not write something, it is undecided.
Thank you for reading to the end!
** 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%
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!
[]: 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