Java Basic Programming Guide | Generated by AI

Home 2025.11

Here’s a focused guide for Part II: Basic Programming of 08553 Introduction to Programming—Java, tailored for the National Higher Education Self-study Examination (自考). This section trains you to write small, correct Java programs and understand how Java handles data and expressions.


1. Reading Input from the Console

1.1 Using Scanner

Java reads input with the Scanner class in java.util.

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.println("Hello " + name + ", you are " + age + " years old.");
        sc.close();
    }
}

Key points:


2. Identifiers, Variables, Expressions, Assignments, and Constants

2.1 Identifiers

Names you give to variables, methods, or classes. Rules:

Examples: studentName, _total, $price

2.2 Variables

A variable holds data of a certain type. Declaration examples:

int age = 20;
double price = 12.5;
char grade = 'A';
boolean passed = true;

2.3 Assignment Statements

Assign a value using = (right → left):

x = 5;
y = x + 2;

2.4 Constants

Declared with final, cannot change later:

final double PI = 3.14159;

Use uppercase names for constants.


3. Numeric Data Types and Operations

3.1 Common Numeric Types

Example:

int count = 5;
double price = 19.99;

3.2 Basic Arithmetic Operators

+, -, *, /, %

Examples:

int a = 10, b = 3;
System.out.println(a / b);  // 3 (integer division)
System.out.println(a % b);  // 1
System.out.println((double)a / b); // 3.3333 (type conversion)

4. Type Conversion (Casting)

4.1 Automatic Conversion (Widening)

Small → large type automatically: intlongfloatdouble

Example:

int i = 10;
double d = i;  // automatic conversion

4.2 Manual Conversion (Casting)

Explicitly convert larger → smaller type:

double d = 9.7;
int i = (int) d; // i = 9 (fraction lost)

Be careful with precision loss.


5. Expression Evaluation and Operator Precedence

5.1 Order of Operations

Java follows a defined order:

  1. Parentheses ( )
  2. Unary +, -, ++, --
  3. Multiplication, division, modulus * / %
  4. Addition and subtraction + -
  5. Assignment =

Example:

int x = 2 + 3 * 4;   // 14, not 20
int y = (2 + 3) * 4; // 20

5.2 Mixed Expressions

If one operand is double, the result becomes double:

double result = 5 / 2;     // 2.0 (integer division first)
double result2 = 5.0 / 2;  // 2.5 (floating-point division)

6. Augmented Assignment and Increment/Decrement

6.1 Augmented Assignment

Shortcut operators:

x += 3;  // same as x = x + 3
y *= 2;  // same as y = y * 2

6.2 Increment and Decrement

++ increases by 1, -- decreases by 1. Two forms:

int a = 5;
System.out.println(a++); // prints 5, then a = 6
System.out.println(++a); // a = 7, then prints 7

Remember:


7. Common Mistakes to Avoid


8. Typical Exam Questions

Example 1:

What is the output of the following code?

int x = 5, y = 2;
double z = x / y;
System.out.println(z);

Answer: 2.0 (integer division before assignment)

Example 2:

Which operator has the highest precedence? A. + B. * C. = D. ++ Answer: D

Example 3:

What is the output of this code?

int a = 3;
int b = ++a + a++;
System.out.println(b);

Explanation: ++a makes a = 4, adds 4, then a++ uses 4 and increments to 5. So b = 8.


Would you like me to continue with Part III: Control Structures (Selection and Looping Statements) next? It covers if, switch, for, while, and is the core of procedural logic in the Java exam.


Back

openai/gpt-5

Donate