PUBLIC LESSON PREVIEW / Java Basics
Variables & Types
Read this sample without an account. Sign in for the full workspace, saved progress and checkpoints. This preview does not save activity or award credit.
What you’ll learn
- Name values, calculate results, and print your first answer.
The concept
A variable is a name attached to a value. Start by naming each piece of information your program needs, then write an expression that combines those values. Python determines a value’s type at runtime; Java asks you to declare a variable’s type, such as int for whole numbers. Both languages support addition, subtraction, multiplication, and parentheses. Choose descriptive names like packs rather than mysterious letters. An assignment stores a result; it does not create a permanent mathematical equation. If a value changes later, an earlier calculation is not automatically repeated. In these examples, sample data is written directly in the program so you can run it immediately. Change one number, predict the new output, and run the code again. That prediction-and-check habit is the foundation of debugging.
Worked example
public class Main {
public static void main(String[] args) {
int packs = 4;
int perPack = 6;
int given = 5;
int remaining = packs * perPack - given;
System.out.println(remaining);
}
}Try thinking it through
Sticker inventory
A club has 4 packs with 6 stickers in each pack. It gives away 5 stickers. Calculate and print the number left.
Example input
packs = 4, per_pack = 6, given = 5
Expected output
19
Hint 1
Calculate the total before the giveaway.
Hint 2
Multiplication happens before subtraction.
Hint 3
Store the remaining count, then print it.
Ready to try it yourself?
ChatGPT sign-in takes you to OpenAI and back to CodeTrail. It keeps your learning account separate from other learners.
Open the full lesson ↗