One, there are two types of data in the Java language
Basic data type: 8 kinds of numeric types such as Int/char
1, byte
The byte data type is a 8-bit, signed, Integer that is represented by a binary complement
The range is -27--(27-1);
The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable occupies only one-fourth of the int type;
Example:byte a = 100,byte B =-50.
2. Short
The short data type is a 16-bit, signed integer with a binary complement representation;
The range is -215--(215-1);
The short data type can also save space as Byte. A short variable is one-second of the space occupied by the int variable;
Example:short s = 1000,short r = 20000
3, int
The int data type is a 32-bit, signed integer with a binary complement representation;
The range is -231--(231-1);
the generic integer variable defaults to the int type;
Example:int a = 100000, int b =-200000
4, long
The Long data type is a 64-bit, signed integer with a binary complement representation;
The range is -263--(263-1);
This type is mainly used on systems that need to compare large integers;
The default value is 0L;
Example: long a = 100000l,long B = -200000l.
"L" is theoretically not case-sensitive, but if written "L" is easily confused with the number "1", it is not easy to distinguish. So it's best to capitalize.
5, float
The float data type is a single-precision, 32-bit, IEEE 754 compliant floating-point number;
Float saves memory space when storing large floating-point groups;
The default value is 0.0f;
Floating-point numbers cannot be used to denote precise values, such as currency;
Example:float f1 = 23.7f.
6, double
The double data type is a dual-precision, 64-bit, IEEE 754-compliant floating-point number;
the default type of floating-point number is double type;
A double type cannot also represent an exact value, such as a currency;
The default value is 0.0d;
Example:double D1 = 3.4.
7, Boolean
The Boolean data type represents the type of the judged data;
There are only two values:True and false, and this type is only used as a flag to record the true/false situation;
The default value is false;
Example:boolean one = True.
8, Char
The char type is a single 16-bit character;
The range is \u0000 (i.e., 0)--\uffff (i.e. 65535);
Char data type can store any character;
Example:char letter = ' A ';
II. Reference data types
Reference type except for the base data type
objects, arrays are reference data types; array int[],string[],
The default value for all reference types is null;
A reference variable can be used to refer to any type that is compatible with it.
Iii. Conversion of data types
In a program where you often encounter situations where you want to convert a data type (for example, when there are different types of operands in an expression), the data type conversion of Java is more restrictive than the C control;
There are two types of data type conversions: Automatic type conversion, forcing type conversion.
1. Automatic type conversion
Data type conversions must meet the following rules:
-
1) Type conversions cannot be made to a Boolean type.
-
2) object types cannot be converted to objects of unrelated classes.
-
3) Forced type conversions must be used when converting large-capacity types to small-capacity types.
-
4) The conversion process may result in overflow or loss accuracy,
For example:
int I =n;
byte b = (byte)i;
Because the byte type is 8 bits and the maximum value is 127, it causes an overflow when cast to the value 128 of the INT type.
5) The conversion of floating-point numbers to integers is obtained by discarding decimals, rather than rounding, for example:
(int) 23.7 = = 23;
(int) -45.9f = =-45;
The number of bits that must be satisfied before the conversion is lower than the converted data type, for example: the number of bits for the short data type is 16 bits, you can automatically convert the int type with the number of bits 32, the same float data type is 32, and can be automatically converted to the double type of 64 bits.
2. Forced type conversion
Coercion type conversions are also referred to as explicit type conversions, with the condition that the data type of the transformation must be compatible
If the data type of the converted value is greater than its target type, some information will be lost;
Such as:
int = 65;
char ch = x;
Such a statement would be an error because the char type is less accurate than the int type, and the compiler cannot convert it automatically, only cast
int = 65;
char ch = (char) x;
Iv. differences between basic types and reference types
V. Assignment of basic data types
Assigning values between basic data types is a copy of the value
Example: int i = 8;
int J = 9;
i = j;
System.out.println ("i=" +i+ "j=" +j);
The result of the operation is I = 9 j= 9.
i++;
j--;
System.out.println ("i=" +i+ "j=" +j);
The result of operation is i = ten j = 8;
This indicates that the values of I and J exist in different spaces, and the respective operations are not affected.
Vi. Assignment of reference types
The assignment of a reference type is called through the new constructor method
Fish fish1 = new Fish ("black", 5kg)
Fish FISH2 = new Fish ("red", 4kg)
Assignment between reference types, which is the assignment between virtual addresses
such as: Fish1 = Fish2, the value of Fish2 to Fish1, refers to the fish2 of the virtual address assignment to FISH1, that is, they will point to the same actual object "Red fish", 4kg.
Both FISH1 and FISH2 will change at the same time, regardless of the new value given to FISH1 or FISH2.
Some summaries of the data types in the Java language