Source: CS 101, Intro to Computer Science, UIUC
Tags: Java, variables, data types, int, double, char, boolean, literals, type declaration, System.out.println, variable assignment, Java basics
Difficulty: Beginner | Prerequisites: None. This is the starting point.
This is where Java programming begins: learning how the language stores and labels data. Every program you write in this course will use variables, so this material is foundational to everything that follows. You need to understand how Java distinguishes between types of data (whole numbers, decimals, characters, true/false) before you can do anything useful with it. If you are joining the course late, start here.
Java requires every variable to have both a type and a name. The type tells Java what kind of data the variable holds (integer, decimal, character, or boolean), and the name is your label for it. You can print output with System.out.println(), assign and reassign values, and use shorthand operators like += to modify variables in place.
Variable
A named container for a value. In Java, every variable must be declared with a specific type before it can be used.
In simple terms, it is a labelled box that holds one piece of data.
Data type
The category of value a variable can hold. Java uses this to determine how much memory to allocate and what operations are valid.
Think of it as the shape of the box: only values of the right shape fit inside.
int
A primitive type for whole numbers (no decimal point). Example: int x = 10; stores the value 10.
In simple terms, use int when you need a number without any fractional part.
double
A primitive type for numbers that can have a decimal point. Example: double x = 10; stores the value 10.0.
Think of it as the "all-purpose number type" for when you need precision or fractions.
char
A primitive type that holds a single character, enclosed in single quotes. Example: char x = 'g';
In simple terms, one letter, digit, or symbol.
boolean
A primitive type that holds only true or false. Example: boolean imGood = true;
Think of it as a yes/no switch.
byte, short, long
Additional integer types with different size ranges. byte is the smallest, short is mid-range, and long is for very large whole numbers.
In simple terms, they are variations of int for when you need a smaller or larger range.
float
A floating-point type like double, but with less precision. double is the default choice for decimal numbers in most situations.
Literal
A value that appears directly in the source code, rather than being computed or read from input. Examples: 10, 1000L, 'g', true.
Think of it as a hard-coded value you type straight into your program.
System.out.println()
A built-in Java method that prints a value to the console, followed by a new line.
In simple terms, it is how your program talks back to you.
To display text or values, use System.out.println("your text here");
The text must be enclosed in double quotation marks
Every statement in Java ends with a semicolon
Every variable needs two things: a type and a name
The name is your identifier for the variable
The type tells Java what kind of data the variable holds
Declaration syntax: type name = value;
Example: int x = 10;
Example: double x = 10; (output when printed: 10.0)
Integer types (whole numbers): byte, short, int, long
Floating-point types (decimals): float, double
Character type: char (single character in single quotes)
Boolean type: boolean (only true or false)
Literals are values written directly into your code. Each type has its own syntax:
Integer literal: 10
Long literal: 1000L (append L to the number)
Character literal: 'g' (single quotes)
Boolean literals: true and false
Examples:
long x = 1000L;
char x = 'g';
boolean imGood = true;
boolean imBad = false;
Once declared, you can change a variable's value without restating the type:
int changing = 10;
changing = 20; // reassign to 20
changing = 20 + 10; // arithmetic on the right side is fine
changing += 1; // shorthand: add 1 to current value
changing /= 2; // shorthand: divide current value by 2
The shorthand operators +=, -=, *=, /= all combine an arithmetic operation with assignment
No formulas for this topic. The key pattern to remember:
type variableName = value;
And the shorthand modification pattern:
variableName operator= value;
// e.g., x += 5; is the same as x = x + 5;
Every piece of software you use relies on variables and types. When a weather app stores today's temperature, it likely uses a floating-point type. When a game tracks your score, it uses an integer. Type safety (Java's insistence that you declare what kind of data you are working with) prevents entire categories of bugs before the program even runs.
Students often think double x = 10; and int x = 10; produce the same output. They do not. Printing a double shows 10.0; printing an int shows 10.
Students often forget the semicolon at the end of a statement. Java will not compile without it.
Students sometimes use double quotes for char values. Characters use single quotes ('g'), while strings use double quotes ("hello").
Students confuse = (assignment) with == (comparison). At this stage, just remember = means "set this variable to this value."
⚠️ Know all eight primitive types and which category each belongs to (integer, floating-point, character, boolean).
⚠️ Be able to trace through a sequence of variable assignments and modifications and state the final value.
⚠️ Understand the difference between int and double output when the same number is stored.
⚠️ Recognise valid and invalid literal syntax (e.g., 1000L for long, single quotes for char).
True or False: In Java, you can declare a variable without specifying its type. False. Every variable must have a type.
Fill in the blank: System.out.println(_____) requires double quotation marks around text and a _____ at the end of the statement. Answer: semicolon
True or False: double x = 5; will print 5 when passed to System.out.println(x). False. It prints 5.0.
True or False: boolean variables can hold values other than true and false. False.
Fill in the blank: The shorthand x = 3; is equivalent to x = x ___ 3;. Answer:
Q: What is the output of the following code?
double x = 10;
System.out.println(x);
A: 10.0. Because x is declared as a double, Java stores and displays it with a decimal point.
Q: What is the value of changing after this sequence runs?
int changing = 10;
changing = 20 + 10;
changing += 1;
changing /= 2;
A: 15. The sequence is: 10 becomes 30, then 31, then 31 / 2 = 15 (integer division truncates the decimal).
Q: Write a line of Java that declares a long variable named bigNum and assigns it the value 5000.
A: long bigNum = 5000L;
Q: Name the four categories of Java primitive types and give one example type for each.
A: Integer types (int), floating-point types (double), character type (char), boolean type (boolean).
This material connects directly to comparisons and conditionals (Lecture 3), where you will use these variable types inside if statements and boolean expressions. It also underpins arrays (Lectures 3 and 4), which store multiple values of the same type. Every subsequent topic in CS 101 assumes fluency with variable declaration and assignment.
Java primitive types, Java variable declaration, int vs double, char single quotes, boolean true false, System.out.println, Java literals, long literal L suffix, compound assignment operators, += -= *= /=, Java basics UIUC, CS 101 intro to computer science, variable modification, type declaration syntax