Data types are abstract representations of memory locations. Programmers can use many data types: some are defined by programming languages, some by external libraries, and some by programmers. Many programming languages rely on specific computer types and specific compilation and implementation of data type attributes, such as word size and integer data types. On the other hand, Java's virtual machine is responsible for defining all aspects of its built-in data types. This means that no matter what underlying operating system the Java Virtual Machine (JVM) runs on, the attributes of data types are exactly the same.
Simple data type
Simple data types are built-in data types that can no longer be simplified. They are defined by programming languages and represent real numbers, characters and integers. Larger and more complex data types can be defined by a combination of simple data types. In most cases, simple data types have their hardware equivalents. For example, simple types of int are sometimes stored in 32-bit hardware registers. Java provides several simple data types to represent numbers and characters.
Simple data types are usually divided into the following categories: real number, integer, character and boolean value. These categories contain many simple types. For example, Java defines two simple types: float and double, which belong to the real number category, while the other four simple types: byte, short, int and long belong to the integer category. In addition, there is a simple type char of character type. There is only one simple type of boolean value category: Boolean. Table A lists the simple data types of Java in detail.
Table A Java simple data types
Simple type size range/accuracy
Floating point 4-byte 32-bit IEEE 754 single precision
Double 8-byte 64-bit IEEE 754 double precision
Byte 1 byte-128 to 127
Short 2 bytes-32,768 to 32,767
Int 4 bytes -2,147,483,648 to 2,147,483,647
8 bytes long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Char 2 byte complete Unicode character set
Boolean 1 bit true or false
Simple data types of Java
All numerical variables in Java are signed, and Java does not allow arbitrary conversion between data types. You can only type numeric variables. For example, boolean cannot be converted to other data types, and other data types cannot be converted to boolean.
Because the simple data type of Java is accurately defined and direct memory access is not allowed, the sizeof operator is cancelled in the Java language.
The simple data type of Java is not an object. In order to handle Java simple data types in an object-oriented way, you need to encapsulate them with classes first.
Encapsulation class
Java also provides built-in encapsulation classes, such as Byte, Short, Boolean, Character, Integer, Double, Float and Long. These packaging classes provide a very intuitive and practical method. For example, Byte, Float, Integer, Long, and Double classes all have a DoubleValue () method, which can be used to convert a value stored in a class instance to a double type. In addition, all encapsulated classes provide static valueOf(String s) methods to convert a given string into the corresponding simple type. The code in Listing A demonstrates some uses of these encapsulated classes.
Simple data type initialization
In the Java language, simple data types are automatically initialized to default values when declared as member variables of a class, unless explicitly declared. When a simple data type declares a local variable for a method, it will not be initialized automatically, and it will cause the compiler to throw an error message similar to the following: "Variable X may not be initialized yet." Table b defines the default values of Java simple data types.
Table b Default value of Java simple data type
type
System default value
boolean type
wrong
byte
short
Internationalorganizations (same as international organizations)
long
tea
0? 7u0000? 0? seven
float
0.0
double;twofold
0.0
Default initial value of Java simple data type
The code in Listing B shows that all Java simple data types are used as member variables of the initialization class. The example also shows that an int variable is partially declared in the constructor of the initialization class. If the above code is not modified, the compiler will throw an error when compiling the above code.
Once the line of code that caused the problem (code that referenced uninitialized variables and caused an error) was deleted or commented out. After the program is successfully compiled and executed, the following results will be displayed:
Bytes: 0
Short: 0
Integer: 0
Length: 0
Floating: 0.0
Double precision: 0.0
Character: 0
Boolean value: false
We can also explicitly initialize member variables to other values, as shown in the following code:
Byte b = 5;;
Short s =123;
int I = 1234;
L =12345;
float f = 123.45 f;
Double d =12345.678;
char c =? 0? 7A? 0? 7;
Boolean z = true;