First, naming rules
1.Java identifiers
All components of Java require a name. Class names, variable names, and method names are called identifiers.
For the Java identifiers, there are a few things to note:
- All identifiers should start with a letter (A-Z or a-Z), a dollar symbol ($), or an underscore (_).
- Can be a combination of any character after the first character
- Keyword cannot be used as an identifier
- Identifiers are case-sensitive
- Examples of legal identifiers: Age, $salary, _value, __1_value
- Examples of illegal identifiers: 123abc,-salary
2. Note:
- case-sensitive : Java is case-sensitive, which means that the identifier Hello is different from hello.
- class name : For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.
- method Name : All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
- source file Name: The source file name must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).
- Main Method Entry : All Java programs are executed by the public static void Main (String args[]) method.
Ii. Types of data
(i) Integral type
1. In the Java language, the range of integer types is independent of the machine in which the program resides (in C language)
Type |
Int |
Byte |
Short |
Long |
Storage requirements |
4 bytes |
1 bytes |
2 bytes |
8 bytes |
Byte and short apply to underlying file processing or storage space considerations
2. In Java, if you want to represent an integer of type long, you need to append a suffix L or l, such as 30L
Octal and hexadecimal representations in 3.Java are the same as in the C language
(b) floating-point type
1.Java floating-point type
Type |
Float |
4 bytes |
Storage requirements |
Double |
8 bytes |
The effective scale of float is 6~7 bit, and the number of effective decimal digits for double is 15 bits
2. By default, the number of floats is double, if you want to represent a float type, you need to add a suffix f or f, such as 3.14F
3.Java scientific notation is the same as the C language
(c) Character type
Char in 1.Java is 2 bytes in Unicode encoding (C language with ASCII encoding)
The escape character in 2.Java is the same as the C language
(d) Boolean type
1.boolean type has only two values: true: true false: False
An integer or pointer in a 2.C language can act as a Boolean value, while Java does not support
Boolean types in Java cannot be converted to other data types
third, variable and constant variable
1. Constant variables: Java declaration constants: using the keyword final
Example: fanal double PI = 3.14;
2. Variable declaration: With C language: TypeName VarName;
Iv. operators
1. Division operator (/): If two operands are integers, the result is a quotient of division (integer division)
If one of the operands is a floating-point number, the result is a float (floating point division)
When the divisor is 0 o'clock, if the divisor is an integer, an exception will be generated, and if the divisor is a floating-point number, an infinity or Nan will be obtained.
2. Plus operator (+): Whenever one operand is a string, string concatenation is performed, and if the other operand is not a string, the system converts it to a string and then operates
3. Modulo operation (%): can be applied to integer type, floating-point number type (only for integer type data in C language)
4. Logical Operators:& and | or ^ xor! Non -
&& Simplification and | | Simplifies non-
The simplification operator does not perform a second operand operation when the result can be obtained from the first operand
5. Bitwise operators: cannot be used with Boolean, float, double, or compound data types, same as C language
There is no comma operator in 6.Java, but you can separate expressions with commas in the first and third parts of the for statement
V. Type conversion
1. If there is a loss of precision in the conversion, it must be done by forcing type conversion (including Int->float float->double, etc.)
Converting an integral type to a character type in 2.Java requires a type cast
Vi. Process Control
1. In the C language, a variable with the same name as the outer nesting can be defined in the inner nesting, at which time the internal definition overrides the external definition and is not allowed in Java
2. In the case of multi-layered nesting, break and continue only act on the current layer of the loop
In Java, if you want to work on the outer loop, you can use tagged break and continue
Example: Outer:
while (J < 100) {
while (I < 20) {
if (i = = 5)
Break Outer;
}
}
If the break statement is not missing in the 3.switch statement, all statements starting with the matching case statement will be executed until the break statement is encountered or until the end of the switch is reached
Java Learning Note -2.java language basics