Assignment & Input
Assignment, One More Time
= always has one variable on the left, and a value (or a math expression) on the right. Read it as "gets assigned," not "equals."
int score = 10 * 2 + 5; // score is assigned the value 25
Copying one variable into another only copies the value at that moment — it doesn't link them together.
int x = 3;
int y = 2;
x = y; // x is now assigned a copy of y's value: 2
y = 5; // y changes — x is still 2, unaffected
Every Variable Needs a Value First
You can't use a variable in an expression before it's been assigned something, and the value has to match the type:
int x = 1;
double y = 2.2;
x = 2 * y; // won't compile — 2 * y is a double, and x is an int
The fix here isn't to force it — it's to make x a double in the first place, since that's what it's actually meant to hold.
Updating a Value Using Itself
A common pattern: change a variable based on its own current value.
int step = 0;
step = step + 1; // uses the OLD value of step, then stores the new result back into it
(Lesson 2.5 covers a shorthand for this: step++.)
Where "Input" Comes From
Some value comes from outside your code, gets stored in a variable, and your code reacts to it. On a robot, that's a joystick or a sensor, not typed text — but it's the same idea: the variable holds whatever value showed up, and the rest of the code doesn't need to change no matter what that value is.
double throttle = 0.6; // stands in for "whatever value the controller sends"
double output = throttle * maxSpeed;
Common Pitfalls
- Using a variable before it has a value. Java won't let this compile — it's a safety net, not an inconvenience.
- Assuming
y = xkeeps them linked. It copies the value once. Changingxlater does nothing toy.
Key Takeaways
- Read
=as "gets assigned," not "equals." - Copying a variable's value into another does not link them — each is independent afterward.
- A variable must have a value, of a matching type, before it's used in an expression.
step = step + 1uses the old value first, then overwrites the variable.- "Input" is any value that comes from outside your code — a joystick reading, a sensor, or typed text.
Derived from csawesome-2026: unit-1-using-objects-and-methods/1.04-assignment-and-input.md (CSA 1.4)