The English version of the summary of knowledge points covered by the questions:
The English version of the summary of knowledge points covered by the questions:
1. The final Keyword and Constants
- When
finalis used on a variable, the variable becomes a constant and can only be assigned once. - After it is assigned, it cannot be changed again. Otherwise, it causes a compile error.
- Constant names are usually written in all uppercase, such as
MAXorPI. - Example:
final int MAX = 100; MAX = 200; // Compile error: cannot assign a value to final variable
Common mistake:
If you see final followed by another assignment, the code will not compile.
2. The + Operator and String Concatenation
+has two meanings in Java:- Between numbers: arithmetic addition;
- If at least one operand is a string: string concatenation.
- Expressions are evaluated from left to right.
- Once a string is encountered, everything after it is concatenated as a string, not added arithmetically.
- Examples:
System.out.println("Sum:" + 5 + 3); // Sum:53 System.out.println("Hello" + 3 + 4); // Hello34 System.out.println(5 + 3 + "5"); // 85 - If you want the addition to happen first, use parentheses:
System.out.println("Sum:" + (5 + 3)); // Sum:8
Common mistake:
Thinking "Sum:" + 5 + 3 will first compute 5 + 3, producing Sum:8.
3. Integer Division and Modulo
- When two
intvalues are divided, the result is still anint. The decimal part is truncated, not rounded. %gives the remainder.- Examples:
System.out.println(7 / 2); // 3 System.out.println(7 % 2); // 1 System.out.println(9 / 4); // 2 System.out.println(9 % 4); // 1 - To get a decimal result, at least one operand must be a
double:System.out.println(7 / 2.0); // 3.5 System.out.println((double) 7 / 2); // 3.5
Common mistake:
Thinking 7 / 2 is 3.5.
4. Operator Precedence and Mixed Expressions
- Precedence:
*,/, and%are higher than+and-. - Operators with the same precedence are evaluated left to right.
- However, string concatenation changes how later operations are evaluated.
- Examples:
int a = 6, b = 4; System.out.println(a + b + " " + a * b); // First a + b = 10, then a * b = 24, then concatenate: 10 24int a = 3, b = 4; System.out.println(a * b + " " + a + b); // First a * b = 12, then after the string, a and b are concatenated: 12 34
Common mistake:
Forgetting that after a string appears, later + operations become concatenation.
5. Type Conversion
1. Automatic Type Conversion (Widening)
- A smaller type goes to a larger type. This is safe and does not require a cast.
- Example:
int a = 5; double b = a; // automatic conversion, b = 5.0
2. Explicit Casting (Narrowing)
- A larger type goes to a smaller type. An explicit cast is required.
- The decimal part is truncated, which may lose precision or cause overflow.
- Example:
double d = 9.99; int x = (int) d; // x = 9 - The following code causes a compile error:
int x = 9.99; // Compile error: possible lossy conversion from double to int int x = d; // Compile error: double cannot be directly assigned to int
Common mistakes:
Forgetting to write (int), which causes a compile error; or thinking (int) 9.99 rounds to 10, when it actually truncates to 9.
6. Three Common Types of Errors
| Error Type | Characteristics | Example |
|---|---|---|
| Compile error | The code violates syntax or type rules and cannot compile | Reassigning a final variable; assigning a double directly to an int |
| Runtime error | The code compiles but crashes while running | Division by zero, array index out of bounds |
| Logic error | The code runs but produces incorrect results | Wrong formula, wrong operator |
7. Output and Newlines
System.out.println(): prints and then moves to a new line.System.out.print(): prints without moving to a new line.\ncan also represent a newline.- Examples:
System.out.print("A"); System.out.print("B"); // Output: ABSystem.out.println("A"); System.out.println("B"); // A and B are on separate lines
8. Review Tips
- When you see
final, check whether it is assigned a second time. - When you see
+with strings, evaluate from left to right and notice where the string appears. - When you see
int / int, the result must be an integer; the decimal part is truncated. - When you see
doublebeing converted toint, an explicit(int)cast is required. - When you see output statements, pay attention to the difference between
printandprintln.
These are the core Java knowledge points behind the questions you missed.

浙公网安备 33010602011771号