Of course, preparations should be made before writing this blog post, such as downloading and installing JDK and setting environment variables, compiling and executing the classic "Hello world ", I have ignored these steps. I will have a lot of introductions using the search engine. I will not go into detail here and go to the question: Native data type.
1. Data Types in Java are classified into two categories
1) Native data type;
2) reference type (object type, which will be described later ).
2. Java Native data types are divided into eight types
1) byte type, represented by byte. (-128 ~ 127 integers between 256) (8 digits)
2) Short integer, represented by short. (16 bits)
3) integer, represented by Int. (32-bit)
4) long integer, represented by long. (64-bit)
5) Single-precision floating point type, represented by float. (Float refers to decimals and real numbers)
6) Double Floating Point type, represented by double.
7) character type, represented by char. (The character type is a single character, which is enclosed in single quotation marks)
8) boolean type, represented by Boolean. (There are two values: true and false)
3. Notes for using native Data Types
1) all floating point types in Java are double by default. Values of the double type cannot be directly assigned to the float variable;
double d = 2.4;float f = d; //Type mismatch: cannot convert from double to float
So how to define a float data?
// The first method is double D = 2.4; float F = (float) D; // you can use the forced type conversion method. // The second method is float F2 = 2.4f; // you can add "F" to the real number"
2) about forced type conversion
Syntax for forced type conversion: type variable name = (type) variable name.
Generally, values with a small range can be directly assigned to variables with a large range. Otherwise, they cannot be forcibly converted. (In later frame learning, as long as reflection is used, forced type conversion is required ).
A is of the int type and B is of the short type. The data range of the int type is larger than that of the short type. You cannot assign a value to a variable with a smaller range, if the conversion is strong, the precision will be lost.
Short B = 3; int A = B; // It can be compiled.
3) variable names
Variable name. in Java, the name of a variable starts with a letter or $ and cannot start with a number.
4) about "=" in Java"
"=" Is to assign the value on the right of the equal sign to the variable on the left.
5) Assignment
Whether the assignment is successful depends on whether the value type on the right of the equal sign is consistent with the variable type on the left of the equal sign.
Because I am not familiar with this editor, I have modified the format several times. PS: Sorting is much more troublesome than taking notes. Next blog, Java basics-operators.