Assignment Statements and Input

An assignment statement stores a value in a variable. A phone's battery level, kept as a percentage, changes through the day.
Initializing and changing a variable
int battery = 80;
battery = battery - 15;
The first line declares battery and stores 80 in it. That first assignment initializes the variable.
The second line has battery on both sides. Java works out the right side before storing anything. It reads battery as it is now, 80, so battery - 15 is 65, and then 65 is stored in battery. The 80 isn't kept anywhere.
A stored value stays
An assignment copies a value at the moment it runs.
int atNoon = battery;
battery = battery - 20;
When the first line runs, battery holds 65, so atNoon gets 65. The second line changes battery to 45. It doesn't change atNoon, which still holds 65, because the second line assigns to battery only.
The type of an expression
The phone used 45 percentage points in 4 hours. A program estimates what the next 2 hours will use at that rate.
double nextTwo = 45 / 4 * 2.0;
System.out.println(nextTwo);
This prints 22.0 and not 22.5. The division comes first, and with an int on each side its value is the int 11. Only then is 11 multiplied by 2.0, giving the double 22.0, and the quarter was already gone. Writing 45 / 4.0 * 2 makes the division a double, and nextTwo gets 22.5.
Compatible types
A double variable accepts an int value, so double level = 80; compiles and holds 80.0. The other direction fails. The line int level = 80.5; doesn't compile, and Java doesn't quietly drop the 0.5.
Text and numbers don't mix either. Since "80" is a String, the line int level = "80"; doesn't compile, even though the characters are digits.
Assigned before use
A variable can be declared on one line and assigned on a later one. Until it has a value, though, an expression can't read it.
int drain;
battery = battery - drain;
This doesn't compile. The variable drain was declared but never assigned, so there's no value for battery - drain to use. Assigning drain a value on the line in between makes it legal.
Reference variables and null
A reference-type variable, like a String variable, can be assigned a new object or the value null.
String status = "Charging";
status = null;
After the first line, status refers to the text "Charging". After the second it doesn't refer to any object, and null is the value that says so.
Input
The battery level is information the program keeps. Input is information a program receives from outside it, and it comes in several forms. Pressing a button on a game controller is tactile input. A voice assistant hearing a question is audio input. A photo added to an app is visual input, and characters typed at a keyboard are text input.
In Java, one way to read what someone types at the keyboard is the Scanner class. The coding problems in this unit that read input already contain those lines.