: Notebook: This article is filed under “blog”
Data type classification
There are two types of data types in Java:
- Value types (also called built-in data types, primitive data types)
- Reference types (all reference types except value types, including
String
, arrays)
Value types
The Java language provides eight basic types, roughly divided into four categories
- integer
byte
– eight.short
– 16.int
– 32.long
– It is a 64-bit character, usually followed by a numberl
或L
.
- floating-point
float
– 32 bits. The value must be appended to a number in direct assignmentf
或F
.double
– It is a 64-bit character. The value is usually assigned after a numberd
或D
。
- character
char
– 16 bits, storing Unicode codes and assigning values with single quotes.
- The Boolean
boolean
– The value can be true or false.
The difference between value types and reference types
- Conceptually
- Basic types: Variable names refer to specific values.
- Reference type: The variable name points to the memory address of the data object.
- In terms of memory
- Basic types: Java allocates memory to a variable immediately after it is declared.
- Reference type: it is declared in a special way (like a C pointer) to an object entity (a concrete value). This type of variable does not allocate memory, but stores a memory address.
- In terms of use
- Basic type: Assign a specific value to use, and use it to judge
= =
Number. - Reference type: null can be assigned when used, but used when judging
equals
Methods.
- Basic type: Assign a specific value to use, and use it to judge
:point_right: extension Read: Java primitive data types and reference types
This article provides a lively description of memory storage for basic data types and reference types.
Data conversion
In Java, there are two ways to convert data types:
- Automatic conversion
- Casts.
Automatic conversion
In general, variables that define a data type cannot be converted at will. But JAVA allows users to do limited type conversions to primitive types.
JAVA will automatically do type conversions if the following conditions are met:
-
From small data to big data
It is obvious that the numeric representation of a “small” data type is less precise than that of a “large” data type.
Therefore, if “big” data is converted to “small” data, data accuracy will be lost. For example, if long is converted to int, data beyond the range represented by int will be lost, leading to uncertainty in the result.
Conversely, when “small” data is converted to “large” data, there is no data loss. For this reason, this type conversion is also called an enlarged conversion.
These types are :(byte, short, char) < int < long < float < double.
By “large” and “small,” we do not mean the number of bytes consumed, but the size of the range of values represented.
-
The data types before and after the conversion should be compatible
Because Boolean types can only store true or false, which are incompatible with integers or characters, type conversions cannot be done.
-
Integer and floating point types are evaluated, and the result is converted to a floating point type
Example:
long x = 30;
float y = 14.3 f;
System.out.println("x/y = " + x/y);
Copy the code
Output:
X/y = 1.9607843Copy the code
You can see that long is more precise than float, but the result is a floating-point number.
Casts.
When automatic conversion conditions are not met or the user needs, the data type can be forced to convert.
Casts use parentheses(a)
。
Reference types can also be cast.
Example:
float f = 25.5 f;
int x = (int)f;
System.out.println("x = " + x);
Copy the code
Packing and unpacking
Packaging, packing, unpacking
Java provides a wrapper class for each of the basic data types, as follows:
Byte <-> byte
Short <-> short
Integer <-> int
Long <-> long
Float <-> float
Double <-> double
Character <-> char
Boolean <-> boolean
Copy the code
The purpose of introducing wrapper classes is to provide a mechanism by which primitive data types can be converted to and from reference types.
The conversion of basic data types to packaging classes is called boxing and unboxing.
packing
Boxing converts a value type to a reference type. Such as:int
转Integer
- The boxing process is done by calling the wrapper class
valueOf
Method.
- The boxing process is done by calling the wrapper class
Split open a case
Unboxing converts a reference type to a value type. Such as:Integer
转int
- The unpacking process is done by calling the wrapper class
xxxValue
Method. (XXX represents the corresponding basic data type).
- The unpacking process is done by calling the wrapper class
Automatic packing, automatic unpacking
Primitive type Boxing unboxing has been available since JDK 5.
Automatic boxing and unboxing makes it easier and more straightforward to use primitive or object types for Java variable assignments or method calls. Because autoboxing implicitly creates objects, it creates useless intermediate objects in a loop body, which can increase GC stress and drag down program performance. So be careful when writing loops to avoid introducing unnecessary autoboxing.
Form prior to JDK 5:
Integer i1 = new Integer(10); // Non-automatic boxing
Copy the code
After JDK 5:
Integer i2 = 10; // Automatic boxing
Copy the code
Java for automatic boxing and unboxing design, rely on a design pattern called the meta pattern (interested friends can go to see the source code, here will not expand on the design pattern).
👉 Extended reading: An in-depth look at boxing and unboxing in Java
Combining with examples, the packing and unpacking principle is described step by step.
The application and attention of packing and unpacking
Application scenarios of packing and unpacking
- One of the most common scenarios is to call aContains the type of
Object
Parametric methodtheObject
Can support any type (becauseObject
Is the parent of all classes) for general use. Used when you need to pass in a value type such as intInteger
Packing. - Another way to use it is: oneNon-generic containers, the element type is defined as
Object
. Thus, to add value type data to a container, you need to box. - when
= =
Operator, where one operand is a wrapper class and the other operand is an expression (that is, contains arithmetic operations) that compares values (that is, triggers automatic unboxing).
Example:
Integer i1 = 10; // Automatic boxing
Integer i2 = new Integer(10); // Non-automatic boxing
Integer i3 = Integer.valueOf(10); // Non-automatic boxing
int i4 = new Integer(10); // Automatic unpacking
int i5 = i2.intValue(); // Non-automatic unpacking
System.out.println("i1 = [" + i1 + "]");
System.out.println("i2 = [" + i2 + "]");
System.out.println("i3 = [" + i3 + "]");
System.out.println("i4 = [" + i4 + "]");
System.out.println("i5 = [" + i5 + "]");
System.out.println("i1 == i2 is [" + (i1 == i2) + "]");
System.out.println("i1 == i4 is [" + (i1 == i4) + "]"); // Automatic unpacking
// Output:
// i1 = [10]
// i2 = [10]
// i3 = [10]
// i4 = [10]
// i5 = [10]
// i1 == i2 is [false]
// i1 == i4 is [true]
Copy the code
Example description:
The above example, while simple, hides the application of automatic boxing, unboxing and non-automatic boxing, unboxing. I1 == i2 is [false and i1 == i4 is [true].
Here’s why:
- I1, i2 are packaging class, use
= =
Java compares them as two objects, not as two ints, so the two objects are naturally not equal. The correct comparison operation should be usedequals
Methods.- I1 is the wrapper class, i4 is the base data type, used
= =
Java automatically unboxing the two i1 wrapper objects into oneint
Theta, and then I plug in theta= =
Calculate in an operational expression; Eventually, it’s twoint
The results are equal because the values are the same.
Packing and unpacking should be paid attention to
- Stuffing operations create objects. Frequent stuffing operations consume unnecessary memory and affect performance. So try to avoid packing.
- Comparison operations for underlying data types
= =
, used for comparison operations of wrapper classesequals
Methods.
summary
The resources
- Java Programming ideas
- JAVA Core Technologies (Volume 1)
- Java base data types and reference types
- An in-depth look at boxing and unboxing in Java