Basic data types and wrapper classes
This is my second article about getting started
Java is a strongly typed language. This means that you must declare a type for each variable: There are eight primitive types in Java. There are four integral types, two floating point types, a char character type for Unicode encoded character units, and a Boolean type for true values.
The integer
Integers are used to represent values that have no fractional part and are allowed to be negative. Java provides four types of integers
type | Storage requirements | Value range |
---|---|---|
byte | 1 byte | -128~127 (-2^7 ~ 2^ 7-1) |
short | 2 – | (2^15 ~ 2^ 15-1) |
int | 4 bytes | -2 147 483 648 -2 147 483 647 (just over 2 billion) (-2^31 ~ 2^ 31-1) |
long | 8 bytes | -2^63 ~ 2^63-1 |
floating-point
Floating point types are used to represent values that have a fractional part. There are two floating point types in Java
type | Storage requirements | The sign bit S | The index of an E | Mantissa bits M |
---|---|---|---|---|
float | 4 bytes | 1bit | 8bit | 23bit |
double | 8 bytes | 1bit | 11bit | 52bit |
- Sign bit: 0 represents a positive number, 1 represents a negative number;
- Exponent bit: The value ranges from 2^-127 to 2^128.
- The mantissa digit is in the form of 1.m or 0.m. Where, when E=0, take 1.m, called normal form, when E! When = 0, take 0.M, called the informal form.
- The mantissa represents the negative power of 2, approximating the decimal scientific notation using binary scientific notation. For float, the mantissa minimum 2^-23 only approximates 10^-7, so it can only accurately represent 6 or 7 decimal places. For double, the mantissa minimum 2^-52 only approximates 10^-16, and only accurately represents 15 and 16 decimal places.
define
Define a float type that must be followed by the suffix F or F. Floating-point values without suffixes are double by default and can also be added with D or D.
float f1 = 3.14 F;
float f2 = 3.14 f;
double d = 3.14;
double d1 = 3.14 D;
double d2 = 3.14 d;
Copy the code
The type char
The Java char is a 16-bit, 2-byte Unicode character set used to represent a single character.
define
char c1 = 'a'; // Add single quotation marks to any single character.
char c2 = 'han'; // Add single quotation marks to any single Chinese character.
char c3 = 126; / / integer. 0 ~ 65535. Decimal, octal, hexadecimal can be. The corresponding character in the output character encoding table.
char c4 = '\u26c5'; // Ranges from \u0000 to \ uFFFF
System.out.println(c1); // a
System.out.println(c2); / / han
System.out.println(c3); / / ~
System.out.println(c4); / / ⛅
Copy the code
Boolean type
Boolean has two values: false and true, which determine that a logical condition integer value cannot be converted to or from a Boolean value.
How many bytes does Boolean take up?
As described in the Java Virtual Machine Specification, the Boolean data type is defined, but only very limited support is provided. There are no special bytecode instructions for Boolean values in the Java Virtual machine. Boolean values operated on by Java language expressions are replaced by Java VIRTUAL machine int data types after compilation. Boolean arrays are encoded as Java virtual machine byte arrays. Each element Boolean element takes 8 bits “. Thus we can conclude that the Boolean type takes up 4 bytes alone and 1 byte in the array.
Type conversions and different type operations
Type conversion
Six solid arrows, indicating the conversion without information loss; There are three dummy arrows that represent conversions with possible loss of accuracy. If the other way around, long goes to int, cast is used.
byte b = 1;
short s = 2;
int i = 3;
long l = 1000;
float f = 1.1 f;
double d = 3.1415926;
s = b;
i = (int) l;
f = i;
f = (float) d;
Copy the code
Different types of operation
- If one of the operands is of type double, the other operand is converted to type double.
- Otherwise, if one of the operands is of type float, the other one will be converted to type float.
- Otherwise, if one of the operands is of type long, the other one will be converted to type long.
- Otherwise, both operands are converted to int.
- In JAVA, char characters are treated directly as integers in the ASCII table when run.
byte b = 1;
short s = 2;
int i = 3;
long l = 1000;
float f = 1.1 f;
double d = 3.1415926;
char c = 'in';
double d2 = b + d; / / 4.1415926
float f2 = s + f; / / 3.1
long l2 = i + l; / / 1003
int i2 = c + i; / / 20016
char c2 = (char) (c + i); / / feng
Copy the code
Packing type
Sometimes, you need to convert a primitive type like int to an object. All primitive types have a corresponding class. Object wrapper classes are immutable, meaning that once a wrapper is constructed, it is not allowed to change the value wrapped inside it. At the same time, object wrapper classes are final, so they cannot be subclassed.
Mapping between basic data types and wrapper types
Basic types of | Packing type |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Automatic packing and unpacking
Boxing automatically converts the base data type to wrapper type; Unpacking automatically converts wrapper types to primitive data types. It can also be automatically boxed and unpacked in arithmetic expressions.
Integer i1 = 3; Integer I = integer.valueof (3);
int i2 = i1; Int i2 = i1.intValue();
Copy the code
The initial value
- The initial value of the integer is 0
- The initial floating-point value is 0.0
- Char starts with a space
- Boolean the initial value is false
- The wrapper types are all NULL
class Obj{
short s;
byte b;
int i;
long l;
float f;
double d;
char c;
boolean bool;
Short so;
Byte bo;
Integer io;
Long lo;
Float fo;
Double dou;
Character co;
Boolean boolo;
}
Obj{s=0, b=0, i=0, l=0, f=0.0, d=0.0, c= , bool=false, so=null, bo=null, io=null, lo=null, fo=null, dou=null, co=null, boolo=null}
Copy the code
The size of the comparison
Integer i1 = 1;
Integer i2 = 1;
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i1 > i2); // false
System.out.println(i1 == i2); // true
System.out.println(i1.compareTo(i2)); / / 0
System.out.println(i3 == i4); // false
System.out.println(i3.equals(i4)); // true
Copy the code
- When using >,<,>=,<= and so on for comparison, the packaging class is automatically unpacked and then compared
- The wrapper class size comparison uses the compareTo method, which returns -1 for less, 0 for equal, and 1 for greater than
- The wrapper class uses == to compare object addresses and equals to compare object values
- Most of the basic types of Java wrapper class implements the constant pool technology, namely, Byte, Short, Integer, Long, Character, and Boolean; Character creates cache data in the range [0,127]. Boolean returns True Or False. New objects are still created if they are out of range.
/** * Integer Cache related code */
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Copy the code
To calculate
When the packing class participates in the calculation, it will be automatically unpacked and calculated. When the calculation result is packed, no new object will be created if it is in the constant pool. New objects are created if they are not in the constant pool.
Integer i1 = 1;
Integer i2 = 1;
Integer i11 = 2;
Integer i3 = 127;
Integer i33 = i3 + 1;
Integer i4 = 127;
Integer i44 = i4 + 1;
Integer i5 = 128;
Integer i6 = i5 - i1;
System.out.println(i11 == i1 + i2); // true
System.out.println(i1 + 1 == i2 + 1); // true
System.out.println(i3 == i4); // true
System.out.println(i3 + 1 == i4 + 1); // true
System.out.println(i33 == i44); // false
System.out.println(i3 == i6); // true
Copy the code
Base data type compared to wrapper type
- In Java, everything is an object, but the eight basic types are not.
- Advantages of the basic type: Simple data storage and high computing efficiency.
- Advantages of wrapping classes: Some containers, such as collections, must have elements of object type, fulfilling the Java idea that everything is an object.
- Basic types do not need to be created with the new keyword, while encapsulated types need the new keyword.
- The basic type is to store the value of the directly stored variable in the stack for efficient access, while the encapsulated type needs to refer to the instance by reference, and the specific instance is stored in the heap.
- The initial value of a wrapper type is null, while the initial value of a basic type depends on the specific type. For example, the initial value of an int is 0, and the initial value of a Boolean is false.
- Differences in usage: for example, when working with collection classes, only wrapper types can be used.