Unit 1 · Lesson 315 minAcademic review pending

Same variables, one new rule underneath

Data types, variables, and scope work almost exactly like C. The one real change is what happens to your program after you type javac — and that change is the whole reason Java exists.

Choose explanation

After this lesson

You should be able to

  • Explain what "write once, run anywhere" means and why bytecode makes it possible.
  • List Java's primitive data types and state which are new compared to C.
  • Apply the same scope and lifetime reasoning from C to Java variables.
01

Compiled once, run everywhere — the one genuinely new idea here

In CS105ES, gcc compiled your C straight to machine code for one specific processor. A program compiled on a Linux machine will not run on Windows without recompiling. Java takes an extra step: javac compiles your source into bytecode, an intermediate form that is not tied to any real processor. Any machine with a Java Virtual Machine installed can then run that exact same bytecode, translating it to real instructions on the fly.

This is where "write once, run anywhere" comes from, and it is why Java became the default choice for anything that had to run on machines its author could not predict in advance — which, in 1995, meant web browsers. The JVM is the one piece of new infrastructure in this whole picture; everything else about compiling and running a program follows the same shape you already know from CS105ES.

Two steps where C had one
// compile: produces HelloWorld.class (bytecode, not machine code)
javac HelloWorld.java

// run: the JVM translates the bytecode for THIS machine
java HelloWorld
02

Data types: mostly the same names, one hard guarantee added

Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. int, double, and char map directly onto what you already used in C. boolean is genuinely new — C had no separate true/false type, and you may recall using int 0 and 1 to fake one. Java makes that distinction real: a boolean can only ever be true or false, and it is not a number at all.

The other change is a guarantee C never made: in C, the size of an int could differ between compilers and machines. In Java, int is always exactly 32 bits, everywhere, on every machine that has ever run Java. That is a direct consequence of write-once-run-anywhere — a guarantee about behaviour has to include a guarantee about size, or the same program would compute different answers on different machines.

03

Scope and lifetime — the CS105ES rules, unchanged

A variable declared inside a block in Java exists only inside that block, exactly as in C — this is the same scope rule you already reasoned about with loop counters and if-branches. A variable's lifetime, how long it holds a value before that memory is reclaimed, also follows the pattern you know: local variables live only as long as the method call that declared them is active.

What is different is what happens after that lifetime ends. In C, you were responsible for freeing memory you allocated yourself with malloc, and forgetting was a real, silent bug. In Java, that entire category of bug disappears — but understanding exactly what replaces it deserves its own lesson, later in this unit, once you have met objects and constructors.

Try it yourself

Write a short Java program with a main method that declares an int, a double, a char, and a boolean, assigns each a value, and prints all four with System.out.println. Do not worry about formatting — just get every type declared and printed correctly.

Need a hint?

The class and file name must match exactly, including capitalisation — Java enforces this, unlike C's freedom to name a .c file anything.

Check the worked solution

The four declarations and their printouts should look almost identical to a C program's variable section, just with System.out.println instead of printf, and semicolons in the same familiar places. The boolean line is the only genuinely unfamiliar one — printing true or false directly, with no %d or %s format specifier needed at all.

public class TypeDemo {
    public static void main(String[] args) {
        int    marks   = 87;
        double average = 76.5;
        char   grade   = 'B';
        boolean passed = true;

        System.out.println(marks);
        System.out.println(average);
        System.out.println(grade);
        System.out.println(passed);
    }
}

Quick check

Why is the exact size of int guaranteed to be 32 bits in Java, unlike in C?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

History of Java, Java buzzwords, data types, variables, scope and lifetime of variables.

Maps to course outcome CO1.