Unit 1.4: Assignment Statements and Input
Assignment and Initialization
Every variable must be assigned a value before it can be used in an expression.
- Initialization: The first time a variable is assigned a value.
- Assignment: Any subsequent change to the variable's value.
Task: Tracking variable state through initialization and updates.
int count; // Declaration
count = 0; // Initialization (first assignment)
System.out.println(count); // Prints 0
count = 5; // Assignment (updating the value)
System.out.println(count); // Prints 5
Reference Types and Null
For reference types (objects), the value stored is the memory address.
nullis a literal that represents a reference that does not point to any object. It is the "empty" state for reference variables.
- If a reference variable does not point to any object, it can be assigned the special value
null. - Literal
null: Indicates that the reference is empty or not associated with any object.
Task: Initializing a reference variable to null.
String s = null; // s doesn't point to any string object yet
The Assignment Operator
The assignment operator = is used to store a value in a variable.
- Syntax:
variable = expression; - The expression on the right is evaluated first, and the result is stored in the variable on the left.
Task: Evaluating an expression before assignment.
int x = 5;
int y = 10;
x = y + 5; // The right side (10 + 5) is 15. x becomes 15.
Strong Typing
Java is strongly typed. The value assigned to a variable must be compatible with the variable's declared type.
- You cannot assign a
doubleto anintwithout casting. - You can assign an
intto adouble(automatic widening).
User Input (Scanner)
You were introduced to the Scanner class in Unit 1.1 as a way to read user input. Recall that Scanner is part of the java.util package and requires System.in to read from the keyboard.
Common Scanner Methods
While you have seen nextLine() and nextInt(), the Scanner class provides methods for reading various data types:
nextInt(): Reads the next token as anint.nextDouble(): Reads the next token as adouble.nextBoolean(): Reads the next token as aboolean("true" or "false").next(): Reads the next token as aString(stops at whitespace).nextLine(): Reads the entire line of text until the user hits Enter.
The nextLine() Trap
A common issue occurs when you use nextLine() immediately after nextInt() or nextDouble(). The numeric methods read the number but leave the "newline" character (created when you hit Enter) in the input buffer. The subsequent nextLine() then reads this leftover newline as an empty line and finishes immediately.
- When numeric tokens are read, the separator (like
\n) remains in the buffer. Programmers must manually clear this buffer before reading text withnextLine().
To fix this: Call an extra nextLine() to consume the leftover newline before reading the actual text.
Task: Correctly handling the nextLine() buffer pitfall.
Scanner input = new Scanner(System.in);
System.out.print("Enter age: ");
int age = input.nextInt();
// Consume the leftover newline
input.nextLine();
System.out.print("Enter name: ");
String name = input.nextLine(); // Now this works correctly