MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
  • Unit 1: Using Objects and Methods
    • Part 1: Fundamentals
      • Introduction to Algorithms, Programming, and Compilers
      • Variables and Data Types
      • Expressions and Output
      • Assignment Statements and Input
      • Unit 1 Part 1 Slides
    • Part 2: Operations and Documentation
    • Part 3: Objects and Classes
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections

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.

The null keyword
  • null is 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 double to an int without casting.
  • You can assign an int to a double (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 an int.
  • nextDouble(): Reads the next token as a double.
  • nextBoolean(): Reads the next token as a boolean ("true" or "false").
  • next(): Reads the next token as a String (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.

Input Buffer Management
  • When numeric tokens are read, the separator (like \n) remains in the buffer. Programmers must manually clear this buffer before reading text with nextLine().

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
Privacy Policy | Terms & Conditions