1 Keyword in Java
Words that are given a specific meaning by the Java language can be called Java keywords.
1.1 Features of Keywords
- All the letters that make up the keyword are lowercase.
- Java has 53 keywords, including two reserved keywords, goto, const. In C/C++, goto is often used to break out of multiple loops. But the use of goto statements tends to make programs less readable, so Java does not allow goto jump. Const defines the role of constants in C, whereas in Java you can define constants using the final keyword. Goto and const exist as reserved words and are not currently used, but may be upgraded to keywords in the future.
- Advanced notepads like Notepad++ have special color markers for keywords, very intuitive.
1.2 Keyword \ Reserved keyword table
53 keywords, including two reserved words.
Keywords for access control modifiers (three total) | ||||
public | protected | private | (the default) | |
Define classes, interfaces, abstract classes and implementation interfaces, keywords for inherited classes, instantiated objects (6 in total) | ||||
class | abstract | interface | extends | implements |
new | ||||
Modify classes, methods, properties, and variables (9 in total) | ||||
static | final | spuer | this | native |
strictfp | synchronized | transient | volatile | |
Program control statements (12 in total) | ||||
if | else | while | for | switch |
case | default | do | break | continue |
return | instanceof | |||
Keywords for exception handling (5 in total) | ||||
try | catch | finally | throw | throws |
Keywords for packages (2 in total) | ||||
package | import | |||
Data types (10 in total) | ||||
float | double | long | int | short |
byte | char | boolean | void | enum |
Other Keywords (4 in total) | ||||
true | false | Assert (claim) | null | |
Reserved words (2 in total) | ||||
goto | const |
2 Data types in Java
2.1 Basic data types
8 bits =1byte
type | Size (bit) | Value range | The default value |
byte | 8 | – 128-127 | (byte)0 |
short | 16 | – 32768-32767 | (short)0 |
int | 32 | – 2147483648-2147483647 | 0 |
long | 64 | – 9233372036854477808-9233372036854477807 | 0L |
float | 32 | + 3.40292347 +38(with valid digits of 6-7) | 0.0 f |
double | 64 | + +308 + 1.79769313486231570 (+ 15 digits) | 0.0 d |
boolean | 1 | true /false | false |
char | 16 | ‘\u0000’ – ‘\ uFFFF ‘(unsigned, converted to decimal is 0-65535) | ‘/uoooo'(null) |
Boolean has only two values: true/false. It can be stored with 1 bit, but the size is not specified. Some JVMS convert Boolean data to int at compile time, using 1 for true and 0 for false. The JVM supports Boolean arrays, but it does so by reading and writing byte arrays.
When a variable is used as a class/object member, Java ensures that its initial value is given to prevent program runtime errors. The default values do not apply to “local” variables (that is, fields that are not of a class).
Floating-point values can be represented in hexadecimal. For example, 0.125 can be represented as 0x0.1P1. In hexadecimal notation, p is used for the exponent, not e. Note that the mantissa is in hexadecimal and the exponent is in decimal. The base of the index is 2, not 10. That is 0.125=0.0625 * 2^1.
All “non-numeric” values in Java are considered to be different. You can’t check whether a particular value is equal to double-.nan like this:
if (x == Double.NaN) // is never true
Copy the code
However, the double-.isnan method can be used:
if (Double.isNaN(x)) // check whether x is "not a number"
Copy the code
2.1.1 Conversion between basic data types
2.1.1.1 Invisible (automatic) conversion
Type ascending. Small data types –> large data types.
During operation, the data will be converted to binary number. The short length of binary number will complement the missing bits relative to the large length of binary number by adding 0, and then carry out operation. So the result of the operation is converted to a large data type rule by default:
If either operand is of type double, the other operand is converted to double; Otherwise, if one of the operands is of type float, the other operand is converted to float; Otherwise, if one of the operands is of type long, the other operand is converted to type long; Otherwise, both operands are converted to int.
Pay attention to
Char b = ‘a’ + 18; Char (s) = char (s) = char (s)
Char a = ‘a’; char b = a + 18; // This is wrong, because the JVM does not know what the result is, so it will tell you the error of losing precision.
2.1.1.2 Forcible Conversion
Type conversion from a large type to a small type.
For example, the ASCII code of A is 65 and the ASCII code of A is 97. This parameter is used when converting the CHAR type.
Syntax for casts: (data type) variables;
Short b=1;
Int c=2;
Short a=(short)(b+c);
Copy the code
Pay attention to
- When casting, you should consider whether the range exceeds the maximum value.
- Data type cast. Data types should be compatible with each other, otherwise accuracy may be lost.
2.1.1.3 case
byte b1=3,b2=4,b;
b=b1+b2; // The type of the variable must be raised before it is calculated.
b=3+4; // If you can't find a constant value, you will not be promoted.
Copy the code
byte b = 130; // There is a compilation problem, it is out of range, it should be forced to transform, but the result is -126 after strong conversion.
Copy the code
short s1 = 1; s1 = s1 + 1; // s1+1 will automatically promote the expression to int, so assigning int to short s1 will cause a conversion compiler error.
short s1 = 1; s1 +=1; //+= is a Java language-specified operator that is treated specially by the Java compiler (converted to the original short type) so that it compiles correctly. Also called implicit type conversion.
Copy the code
2.2 Compound Types (Reference Types)
Array: int [] arr;
Classes, custom classes: class, abstract class
String: Special reference type (immutable)
Interface, enumeration, null type