1.4 - Assignment Statements and Input
Every variable has to be assigned a value before you can use it in an expression, and that value has to match the variable’s data type. The first time you assign a variable a value, that’s called initializing it.
int count; System.out.println(count); // won't compile, count was never given a value int total = 3.5; // won't compile, a double can't be stored in an int double average = 90; // fine, the int 90 is stored as 90.0
A value is compatible with a variable when it’s the same type or can be safely converted, like an
int going into a double. Going the other way (a double into an int) needs a cast, which is covered in 1.5.Reference type variables can be assigned an object, or
null if there’s no object to point to. null just means “this reference isn’t pointing to anything.”String name; // declared but not initialized name = "Alex"; // now it's initialized String nothing = null; // explicitly points to nothing
The assignment operator
= stores whatever’s on the right side into the variable on the left. Note: don’t chain assignments together like a = b = 4;, that’s outside the scope of the course.The
= sign doesn’t mean “equals” like in math, it means “store.” Java evaluates the whole right side first, using the variables’ current values, then stores the result in the variable on the left. That’s why x = x + 1 makes sense: it takes the current x, adds 1, and stores the new value back in x. The old value is gone.Tracing assignments line by line is a big part of the exam. For example, here’s how to swap two variables using a temporary one:
int a = 3; int b = 8; int temp = a; // temp = 3 a = b; // a = 8 b = temp; // b = 3
Without
temp, writing a = b; and then b = a; would leave both variables equal to 8, since the original 3 gets overwritten first.During execution, an expression gets evaluated down to a single value, and that value has a type based on how the expression was evaluated.
double result = 7 / 2 + 1.5; // 7 / 2 is the int 3, then 3 + 1.5 is the double 4.5 int sum = 4 + 2 * 3; // evaluates to the single int value 10
Programs can get input in many forms: tactile (touch, like pressing a button), audio (a microphone), visual (a camera), or text (typing on a keyboard). The
Scanner class is one way to get text input.To read input from the keyboard, you’ll use the
Scanner class.import java.util.Scanner; Scanner scanner = new Scanner(System.in); int age = scanner.nextInt();
Here’s a full program that reads a name and an age:
import java.util.Scanner; public class Greeting { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("What's your name? "); String name = input.next(); System.out.print("How old are you? "); int age = input.nextInt(); System.out.println("Hi " + name + ", next year you'll be " + (age + 1) + "."); input.close(); } }
If the user types Ana and then 16, the screen shows:
What's your name? Ana How old are you? 16 Hi Ana, next year you'll be 17.
Note: the AP exam doesn’t test any specific way of getting input from the user.
Scanner comes back in 4.6, where it’s used to read text files.