Basic Java review-data types, variables, and Arrays
"Learning and learning, not to mention !"
Java basic type
The width of a medium-sized type should not be regarded as the number of storage locations of his Korean, but should be understood as defining this type
Type. As long as the type complies with the declaration rules, the Java runtime environment can be freely used
Small (Space ).
Integer
Name width range
Long 64-9 223 372 036 854 775 808 ~ 9 223 372 036 854 775 807
Int 32-2 147 483 648 ~ 2 147 483 647
Short 16-32 768 ~ 32 767
Byte 8-128 ~ 127
Floating Point Type
Approximate name width range
Double 64 4.9e-324 ~ 1.8e + 308
Float 32 1.4e-045 ~ 3.4e + 038
Character Type
Name width
Char 16
Java uses Unicode to represent characters. Unicode is the unity of many character sets and requires a 16-bit width.
Hold: more information about Unicode can be found in the http://www.unicode.org.
Program:
// Char variables behave like integers.
Class chardemo2 {
Public static void Mian (string [] ARGs ){
Char success;
Rows = 'X ';
System. Out. println ("CH1 contains" + outputs );
Else ++;
System. Out. println ("token is now" + token );
}
}
Output:
CH1 contians x
Token is now y
Although the operator type is positive, it can also be regarded as an integer when performing arithmetic operations.
Variable Scope
When nesting a variable, note that the name of the variable cannot be the same as that of the variable in the external scope.
Program:
// This program will not compile
Class scopeerr {
Public static void main (string [] ARGs ){
Int bar = 1;
{
Int bar = 2; // compile-time error-bar already defined!
}
Java type conversion
Forced conversion of incompatible types
The conversion can be performed automatically when the value is changed from small to large. However, if the value is changed to small, the conversion must be forced.
For example, when an int is forcibly converted into a byte, if the integer value exceeds the byte range, it is modeled in the byte range (that is, an integer is used ).
Value divided by the remainder of the byte range.
Program:
// Demonstrate casts.
Class conbersion {
Public static void main (string [] ARGs ){
Byte B;
Int I = 257;
Double D = 323.142;
System. Out. println ("/nconversion of int to byte .");
B = (byte) I;
System. Out. println ("I and B" + I + "" + B );
System. Out. println ("/nconversion of double to int .");
D = (INT) I;
System. Out. println ("D and I" + D + "" + I );
System. Out. println ("/nconversion of double to byte .");
D = (byte) B;
System. Out. println ("d and B" + D + "" + B );
}
}
Output:
Conversion of int to byte.
I and B 257 1
Conversion of double to int.
D and I 323.142 323
Conversion of double to byte.
D and B 323.142 67
When the value 257 is forcibly converted into a Byte variable, the result is the remainder of 257 divided by 256 (the byte range), that is, 1 ......
Automatic type upgrade
Byte A = 40;
Byte B = 50;
Byte c = 100;
Int d = a * B/c;
The result of a * B is easily out of the byte operand range. To solve this problem, Java automatically calculates the byte
, Short or char operands are upgraded to int. Therefore, when expression a * B is executed, the integer type is used instead of the byte type.
Although automatic upgrade is useful, compilation errors may also occur.
For example:
Byte B = 50;
B = B * 2; // error! Cannot assign an int to byte!
In this B * 2 expression, B has been promoted to the int type, and the result is of course the int type. errors may occur if the value is paid to the byte type variable.
If you can understand the consequences of overflow, use the following force conversion:
Byte B = 50;
B = (byte) B * 2;
Array
Elements in an array with a new value are automatically initialized to 0.