← Skill tree CS Skill Tree 0 CSCD210

Variables and the Primitive Types

Textbook: BJP (Reges and Stepp)

Where you are: Week 0 review > Variables and the primitive types

Try This First

A friend who knows Python says: “I can write x = 5 and later x = 'hello' and it works.” Decide whether the same two lines work in Java before reading on.

Reveal

No. Once you declare int x = 5;, the name x is permanently an int. Assigning text to it is a compile error. Java fixes the type at declaration and never changes it.

Before You Start

Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.

See How a Java Program Compiles and Runs if either box is unclear.

Not sure? Take the 60-second self-check.

Try each from memory, then read the answer under it.

  1. Which file does the JVM run, the one you typed or the one the compiler produced? The one the compiler produced, the .class bytecode. You edit and keep the .java source.
  2. You change a source file and run it without recompiling. Which version runs? The old one. The launcher runs whatever .class is on disk, so you compile again first.

What You Need To Walk In With

Walk into the next class able to state these:

You should be able to: classify a literal as int, double, char, String, or illegal; trace a short sequence of assignments and state the exact output.

How It Works

Writing int age = 20; does three things at once: it reserves memory for a whole number, labels that memory age, and stores 20 in it. The type label is permanent; the value inside can change.

int age = 20;
double gpa = 3.75;
boolean enrolled = true;
char section = 'A';
String name = "Alex";
Type Holds Example
int Whole numbers, about negative two billion to positive two billion int age = 20;
double Decimal numbers, about 15 significant digits double gpa = 3.75;
boolean true or false, nothing else boolean enrolled = true;
char One character, in single quotes char section = 'A';
String Text (an object, not a primitive) String name = "Alex";

= means gets

int x = 5;   // x gets 5
x = x + 1;   // x gets the old value of x plus 1, so x is now 6

Read = as gets. The right side is evaluated, and the result is stored on the left. It is a command, not a claim that the two sides are equal.

Declaration without a value

int count;
System.out.println(count);   // compile error: variable count might not have been initialized

A local variable starts with no value. Assign one (int count = 0;) before you read it.

A constant that does not change

final double TAX_RATE = 0.08;

final marks a value that must not be reassigned. The convention names constants in all capitals.

Worked Example: Predict, Then Check

int x = 10;
x = x + 5;
System.out.println("x: " + x);
char grade = 'B';
System.out.println("grade: " + grade);

Predict both lines of output before reading on.

Reveal
x: 15
grade: B

x + 5 is computed as 15 and stored back into x. The char 'B' joined to a String with + becomes the text B.

A Common Mistake

Using double quotes for a single character is a frequent error: char letter = "A"; does not compile, because "A" is a String and a String does not fit in a char box. Single quotes make a char ('A'); double quotes make a String ("A"). When a compiler message mentions a char literal or a String literal, check the quote style first. (Source: BJP (Reges and Stepp), Ch 2; JLS 25, §3.10.4 and §3.10.5.)

Go Deeper (optional)

For the curious: a char is really a 16-bit number underneath. char c = 'A'; System.out.println((int) c); prints 65, the code point for A. And int stops near two billion because 32 binary digits can represent only so many values, the same place-value idea as base ten, with base two instead. (This connects to how the real number system is modeled in computing.)

Check Yourself

Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.

Check your understanding

Classify the literal 3.14 .

Tier 1 · BJP (Reges and Stepp), Ch 2

What happens when you compile char letter = “A”; ?

Tier 2 · JLS 25, 3.10.4 and 3.10.5

int x = 10; x = x + 5; System.out.println(x); What prints?

Tier 1 · BJP (Reges and Stepp), Ch 2

int count; then System.out.println(count); with no assignment between. What happens?

Tier 2 · JLS 25, 16 (Definite Assignment)

Which type holds only true or false?

Tier 1 · BJP (Reges and Stepp), Ch 2