Lesson 2 of 914 min2 challenges

Variables and Types: Say What It Is First

int/double/String, the integer division trap, and final constants.

In Java you say what a thing is before you use it

In Python you write x = 5 and move on. Java wants one more word:

int age = 18;
String name = "Maya";
double price = 19.9;
boolean isFree = true;

The shape is always type name = value;.

Why the extra word? Because it's a promise you make to the compiler, and the compiler holds you to it. Write int age and then later write age = "eighteen"; and the program refuses to compile:

incompatible types: String cannot be converted to int

That's the whole bargain. You spend one word now; the compiler stops that mistake from ever reaching a user. In Python the same mistake sails through until some unlucky line tries to do arithmetic on the word "eighteen."

A useful way to think about it: a variable in Java is a labeled box of a fixed size and shape. An int box only accepts whole numbers. You can't cram text into it, and the compiler is the inspector who checks.

The types you'll actually use

Java has eight primitive types. In real beginner code you'll use four.

int count = 100;                 // whole numbers, roughly ±2.1 billion
long bigNumber = 10000000000L;   // bigger whole numbers — note the L
double price = 19.99;            // decimals; this is the default choice
boolean ok = true;               // only true or false, never 1 or 0

That L on the end of 10000000000L isn't decoration. Without it, Java reads the digits as an int first, notices the number doesn't fit, and errors out before it ever gets to the long on the left. The L says "read this as a long from the start."

The other four — byte, short, float, char — show up in specialized work. Just recognize the names. One of them has a quirk worth knowing now:

char grade = 'A';

char holds exactly one character and uses single quotes.

String is different, and the capital letter is the clue

String name = "Maya";

String starts with a capital letter because it's a class, not a primitive. Every class name in Java is capitalized, so capitalization is a reliable signal about what you're looking at.

Strings need double quotes. 'abc' is a compile error in Java — single quotes mean "one character," and abc is three.

String a = "hello";    // fine
char b = 'h';          // fine
String c = 'hello';    // error: unclosed character literal

The integer division trap

This one catches everybody, so read it twice.

System.out.println(7 / 2);       // 3
System.out.println(7.0 / 2);     // 3.5
System.out.println(7 / 2.0);     // 3.5
System.out.println(7 % 2);       // 1

Line by line: 7 / 2 has an int on both sides, so Java does integer division — the answer must be an int, and the .5 is chopped off, not rounded. Make either side a decimal and Java switches to decimal division. The last line is %, the remainder operator: 7 divided by 2 leaves 1 left over.

The wrong mental model: beginners assume Java looks at where the result is going and picks a mode accordingly. It does not. Java looks only at the two operands. So this is a genuine, working-as-designed disappointment:

double result = 7 / 2;
System.out.println(result);      // 3.0

It prints 3.0, not 3.5. The division ran first, in int mode, producing 3. Only then was that 3 widened into a double on its way into the box. The double on the left arrived too late to help.

To fix it, change one of the operands before the division happens:

int a = 7, b = 2;
double result = (double) a / b;
System.out.println(result);      // 3.5

(double) a is a cast — it tells Java to treat a as a decimal for this expression. Now the division is decimal division and you get 3.5.

Watch the parentheses, though:

System.out.println((double) (a / b));   // 3.0

That prints 3.0. The parentheses made the int division happen first, and casting afterwards can't recover a digit that's already gone.

String concatenation

String name = "Maya";
int age = 18;
System.out.println(name + " is " + age + " years old");

Prints:

Maya is 18 years old

When + meets a String, it stops meaning addition and starts meaning "glue these together." Numbers get converted to text automatically. Convenient — and the source of a classic surprise:

System.out.println(1 + 2 + " years");   // 3 years
System.out.println("Age: " + 1 + 2);    // Age: 12

The rule is that + runs strictly left to right. Line one starts with 1 + 2, both numbers, so that's real addition: 3. Then 3 + " years" involves a String, so it glues: 3 years.

Line two starts with "Age: " + 1. A String is already involved, so it glues into "Age: 1". Then "Age: 1" + 2 glues again into "Age: 12". The 1 and 2 never got a chance to be added — by the time they met, the expression was already text.

If you want the sum, say so with parentheses:

System.out.println("Age: " + (1 + 2));   // Age: 3

final: values that can't move

final double PI = 3.14159;
// PI = 3.14;   ← will not compile

final means "assigned once, then locked." The second line produces cannot assign a value to final variable PI.

By convention, constants are named in ALL_CAPS_WITH_UNDERSCORES, so they stand out when you're reading.

Use final for anything that shouldn't change: a tax rate, a maximum size, a configuration value. It costs you nothing and turns "someone reassigned this by accident" from a debugging session into a compile error.

Naming conventions

The Java community is unusually unified about naming. Follow it and your code looks like everyone else's, which is the goal.

  • Variables and methods: camelCaseuserName, getTotalPrice
  • Classes: PascalCaseMain, UserService
  • Constants: ALL_CAPSMAX_SIZE

Breaking these won't cause an error. It will make every reader of your code — including you in three months — pause and squint.

Ten-second check

int x = 10;
int y = 3;
System.out.println(x / y);

The answer is 3, not 3.333 and not 4. Both operands are ints, so Java does integer division and throws away the fractional part without rounding.

Ad slot (AdSense not configured; ads appear here in production)

Hands-on

Reading it isn't knowing it. Writing it is.

0/2 passed

Challenges and grading are completely free, and a wrong answer costs you nothing. Stuck? Hit "Ask the AI": it can see the code you are writing.

Level 01Guess the output+10 pts · +15 XP

Integer division

What does this print? ```java int x = 10; int y = 3; System.out.println(x / y); ```

  • Might be multiple choice. Wrong answers cost nothing, so just try.
Level 02Guess the output+10 pts · +15 XP

When does + mean "glue together"?

What do these two lines print? ```java System.out.println(1 + 2 + " years"); System.out.println("Age: " + 1 + 2); ```

  • Might be multiple choice. Wrong answers cost nothing, so just try.

Finished? Mark it

Marking it done saves your progress, gives you 20 XP and keeps your streak alive.