Java Learning note 01--data types
Java Data type Partitioning
are divided into two main types:
1) Basic data type: similar to normal values.
2) Reference data type: The address of the memory is passed.
Floating-point types are actually representing decimals.
Java Basic data types
Overflow of data
When the data size of an integer exceeds the range that can be represented, and the value range is not checked in the program, the values output by this integer variable will be confused and not expected to run.
Example: Finding the maximum value of an integral type
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- int max = Integer.max_value;
- System.out.println ("The maximum value of the integer type is:" +max); the maximum value for the//integral type is: 2147483647
- }
- }
Now the maximum value to be asked for the addition operation
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- int max = Integer.max_value;
- System.out.println ("The maximum value of the integer type is:" +max); the maximum value for the//integral type is: 2147483647
- System.out.println ("Maximum value of integer + 1:" + (max+1)); maximum value of//integer + 1: -2147483648
- System.out.println ("Maximum value of Integer + 2:" + (max+2)); maximum value of//integer + 2: -2147483647
- }
- }
If you want to avoid data overflow now, you can expand the data type in a way. Int-->long
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- int max = Integer.max_value;
- System.out.println ("The maximum value of the integer type is:" +max); the maximum value for the//integral type is: 2147483647
- System.out.println ("Maximum value of integer + 1:" + (max+1)); maximum value of//integer + 1: -2147483648
- System.out.println ("Maximum value of Integer + 2:" + (max+2)); maximum value of//integer + 2: -2147483647
- SYSTEM.OUT.PRINTLN (maximum value of integer + 2: "+ (long) max+2)); //2147483649
- }
- }
Character type
The character type occupies 2 bytes in memory and can be used to hold characters such as English letters. When a computer handles character types, it treats these characters as different integers.
Therefore, strictly speaking, the character type is also considered as one of the integer types.
[Java]View Plaincopy
- Public class T {
- public static void Main (string[] args) {
- char ch1 = ' a '; the//character is data enclosed in '
- char CH2 = 97; //define character variables by numbers
- System.out.println ("ch1 =" +ch1);
- System.out.println ("CH2 =" +ch2);
- }
- }
Commonly used escape characters
Floating-point type and double-precision floating-point number type
In daily life, the number of decimal types is often used, such as height, weight and so on, the whole number can not meet the requirements of the program designer. In mathematics, these values with a decimal point
Called a real number, in Java, this data type is called a floating-point type (FLOAG) with a length of 32 bytes and a valid range of -3.4E1038 to 3.4E1038. When the range of floating-point numbers is not large enough
There's also a double-precision (double) floating-point number to use. Double-precision floating-point number type is 64 bytes long and valid from -1.7E10308 to 1.7E10308
In Java, a number or a decimal actually has a default type:
The default type for decimals (1.1,1.2) is double type
The default type of integer (All-in-one) is type int
Boolean type
Boolean (Boolean) type of variable, only true (true) and False (false) two
Default values for basic data types
Java Learning note 01--data types