Classification of data types
-
Basic data type
-
Integer type
The types of integer data described in Java language include byte, short, int, long, and int
- The byte type takes up 1 byte in the memory space, and the representation range is: - 2 ^ 7 ~ 2 ^ 7-1
- The short type occupies 2 bytes in the memory space, and the representation range is: - 2 ^ 15 ~ 2 ^ 15-1
- The int type takes up 4 bytes in the memory space, and the representation range is: - 2 ^ 31 ~ 2 ^ 31-1. •
- The long type occupies 8 bytes in the memory space, and the representation range is: - 2 ^ 63 ~ 2 ^ 63-1
- matters needing attention
- Integer data written out directly in Java programs is called direct quantity / literal value / constant, which is of type int by default. If you want to express a larger direct quantity, add L or l after the direct quantity, and l is recommended.
- If the initialization of a variable declaring a long type is larger than the long type, use Java math. BigInteger
Code demonstration
/* Programming the use of integer type */ public class IntTest{ public static void main(String[] args){ // 1. Declare a command of byte type and initialize it byte b1 = 25; // 2. Print the value of the variable System.out.println(b1); System.out.println("-------------------"); // byte b1 = 128; Error: incompatible type: conversion from int to short may be lost short s1 = 128; // short s1 = 250250; Error: incompatible type: conversion from int to short may be lost System.out.println(s1); System.out.println("-------------------"); int i1 = 250250; // int i1 = 2502505006; Error: too large integer: 2505006 // int i1 = 2502505006L; Error: incompatible type: conversion from long to int may be lost System.out.println(i1); long l1 = 2502505006L; System.out.println("-------------------"); System.out.println(l1); // If the initialization of a variable declaring a long type is larger than the long type, use Java math. BigInteger // Is there any mistake in the line and surface code? If yes, please indicate and explain the reason // int i2 = 25; // byte b2 = i2; // System.out.println("b2" = b2); // Error: incompatible type: conversion from int to byte may be lost } }
-
Floating point type (decimal)
The types used to describe decimal data in Java language are float and double. The double type is recommended
- The float type takes up 4 bytes in the memory space, which is called a single precision floating-point number. It can represent 7 significant digits. The range is: - 3.403E38~3.403E38.
- The double type occupies 8 bytes in the memory space, which is called double precision floating-point number, and can represent 15 significant digits. The range is: - 1.798E308~1.798E308.
- matters needing attention
- The decimal data written out directly in Java program is called direct quantity, and the default is double type. If you want to express the direct quantity of float type, you need to add f or F after the direct quantity
- The significant bit indicates that the number after exceeding is not within the precise range, and the value will deviate
- If you want to calculate money, use Java math. BigDecimal type
Code demonstration
/* Programming the use of floating point type */ public class DoubleTest{ public static void main(String[] args){ // 1. Declare a variable of type float and initialize it // float f1 = 3.1415926; Error: incompatible type: conversion from double to float may be lost float f1 = 3.1415926F; // 2. Print the value of the variable System.out.println(f1); // 3.1415925 seven significant digits by default System.out.println("---------------------"); Double d1 = 3.1415926; // 3.1415926 default 15 significant digits System.out.println(d1); // 4. Written examination site System.out.println(0.1 + 0.2); //0.3000000000000004 operation may cause errors. If you want money operation, use Java math. BigDecimal type } }
-
Boolean type
-
The types of information used to describe true and false in Java language are boolean, and the values are only true and false.
-
The size of boolean type in memory space is not clearly specified and can be considered as 1 byte.
-
Code demonstration
/* Programming the use of Boolean types */ public class BooleanTest{ public static void main(String[] args){ // 1. Declare a boolean variable boolean b1 = true; // boolean b1; // Error: variable B1 may not have been initialized // 2. Print the value of the variable b1 = false; System.out.println(b1); // Note that in java, boolean has only true and false // b1 = 1; Error: incompatible type: int cannot be converted to boolean } }
-
-
Character type
-
The data type used to describe a single character in the Java language: char type.
-
The char type occupies 2 bytes in the memory space and has no sign bits. The range of representation is 0 ~ 65535,
-
matters needing attention
In real life, few data can be described by a single character, so in future development, more strings composed of multiple strings are used and described by String type,
-
Code demonstration
/* Programming the use of character types */ public class CharTest{ public static void main(String[] args){ // 1. Declare a variable of type char and initialize it char c1 = 'a'; // 2. Print the value of the variable System.out.println(c1); System.out.println("The corresponding number is" + (int)c1); // Indicates that c1 of char type is forcibly converted to int type and printed System.out.println("The corresponding number is" + 'a'); System.out.println("--------------"); char c2 = 98; System.out.println(c2); System.out.println("The corresponding number is" + (int)c2); } }
-
-
ASCII
The bottom layer of the computer only recognizes the binary sequence composed of 0 and 1, which does not meet the rule for patterns such as character 'a', so the data cannot be directly stored in the computer, but in real life, such pattern data needs to be stored by the computer. In order to store the data, a number can be assigned to the data, Then store the number, which is called ASCII.
-
ASCII table
-
ASCII required:
'0' - 48 'a' - 65 'a' - 97 spaces - 32 line breaks - 10
-
-
-
Reference data type
Array, class, interface, enumeration, annotation
-
Conversion between basic data types
-
Automatic type conversion
Automatic type conversion mainly refers to the conversion from small type to large type
-
Conversion rules
-
Code demonstration
/* Automatic type conversion */ public class AutoTransformTest{ public static void main(String[] args) { // 1. Define two variables of different types and initialize them byte b1 = 10; short s1 = 20; // 2. Print the values of two variables System.out.println("b1 = " + b1); // b1 = 10 System.out.println("si = " + s1); // si = 20 System.out.println("----------------------"); // 3. Automatic data type conversion s1 = b1; System.out.println("b1 = " + b1); // b1 = 10 System.out.println("si = " + s1); // si = 10 } }
-
-
Cast (use with caution)
Forced type conversion mainly refers to the conversion from large type to small type
-
Syntax format
Target type variable name = (target type) source type variable name;
-
Code demonstration
public class TransformTest{ public static void main(String[] args) { // 1. Define two variables of different types and initialize them byte b1 = 10; short s1 = 20; // 2. It means that the value of s1 is given to the variable b1 and overwrites the original value of b1, which is equivalent to the conversion from short type to byte type from small to large and forced conversion // b1 = s1; Error: incompatible type: conversion from short to byte may be lost b1 = (byte) s1; System.out.println("b1 = " + b1); // b1 = 10 System.out.println("si = " + s1); // si = 10 System.out.println("----------------------"); // 3. Data loss s1 = 128; b1 = (byte) s1; System.out.println("b1 = " + b1); // b1 = -128 System.out.println("si = " + s1); // si = 128 } }
-
-