KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Variables and Types : 08/25/2026

  • Welcome to CS 124 : 08/24/2026

Variables and Types

int i = 0;
System.out.println(i);
char c = 'A';
System.out.println(c);

Now let’s begin talking about the basic building blocks of computer programs. We’ll show how computers can store data using variables, and discuss how Java distinguishes between different types of data.

What Computers Are Good At
What Computers Are Good At

Computers represent the most powerful problem-solving tool humans have ever created. Part of what makes them so useful is that they are good at things that humans are not.

Over the next few lessons we’ll be discussing several core computer capabilities:

These abilities form the foundation of everything that computers can do. Even if computers sometimes seem complex—they are actually quite simple. (That doesn’t mean that computer programming or computer science is simple or easy, far from it!)

Variables
Variables

Computers are great at storing information or data. Let’s look at how we can store a single number in a Java program:

int i = 0;

This code does three things:

  1. It tells the computer that we are going to store data in a variable named i
  2. It tells the computer that the variable i will store integer data (int)
  3. It sets the initial value of i to be zero

We have declared our first variable! As its name implies, a variable’s value can and usually does change as the program runs.

int i = 0;
System.out.println(i);

Declaration and Initialization
Declaration and Initialization

Technically the statement int i = 0; is combining two things that we can do separately: variable declaration and initialization. Let’s write it out on two separate lines:

int i; // I'm going to use a variable named i to store integers
i = 0; // I'm setting its initial value to zero
System.out.println(i);

While we can do it this way, Java will complain if we don’t set an initial value:

int i;
// Excuse me... what's the initial value of i?
System.out.println(i);

Run the code above and see what happens. As a result, it’s more common to see the variable declaration and initialization combined into a single statement.

int i = 0;
System.out.println(i);

Literals
Literals

In the code above we also used our first literal—the value 0 that we used to initialize i. A literal is a value that appears directly in your code. Unlike a variable, its value does not change.

int i = 10; // 10 is a int literal, i is a variable
double threshold = 0.5; // 0.5 is a double literal, threshold is a variable
boolean isACat = true; // true is a boolean literal, isACat is a variable
char favorite = '8'; // 8 is a char literal, favorite is a variable

Note that in Java, character literals must be enclosed in single (not double) quotes. So this won’t work:

char favorite = "8"; // Oh so close!

Types
Types

All data in Java is represented by combinations of 8 different types of value. These are known as the Java primitive types.

Here are all 8 Java primitive types, broken into four categories:

  1. Integer values: byte, short, int, and long
  2. Floating point values: float and double
  3. Boolean values: boolean
  4. Characters: char

Integer Values
Integer Values

int bigValue = 88888888;
System.out.println(bigValue);

Java has four different primitive types for representing integer values. An integer is a number without a decimal point. So 0, 8, 16, and 333 are all integers, but 8.7, 9.001, and 0.01 are not.

Why does Java have four different types for representing the same kind of data? Because each can hold a different range of values. byte variables can store values from -128 to 127, while int variables can store values from -2,147,483,648 to 2,147,483,647: But int variables also take up more computer memory. Don’t worry too much about these distinctions now. We’ll almost always use int variables to store integers in this class.

Floating Point Values
Floating Point Values

double temperature = 82.4;
System.out.println(temperature);

Java provides two types for storing decimal values: float and double. Similar to with integer values, float variables can store a smaller range of values than double variables. We’ll commonly use a double when we need to store a floating point value.

Boolean (or Truth) Values
Boolean (or Truth) Values

boolean fall2026Rules = true;
System.out.println(fall2026Rules);

In order to make decisions about what to do in our programs, we’ll frequently want to determine whether something is true or false. Variables with Java’s boolean type can store only two values: true and false.

Character Values
Character Values

char catsInitial = 'X';
System.out.println(catsInitial);

Finally, Java provides the char type for storing a single character value. And while it’s uncommon to store single character values, it’s very common to store multiple character values. We’ll return to that type of data later.

Practice: Solve Practice with Primitive Types

Created By: Geoffrey Challen
/ Version: 2020.6.0

Write a snippet of code—not a function—that:

  • declares a variable count of type int and initializes it to 88
  • declares a variable named temperature of type double and initializes it to 14.3
  • declares a variable letter of type char and initializes it to X
  • declares a variable named isCSAwesome of type boolean and initializes it to true

Why Types?
Why Types?

Not every programming language requires that you indicate what type of data a variable will hold. For example, this is valid code in the Python programming language:

i = 0 # Now i is storing an integer...
i = 10.8 # ...but now it's storing a floating point value!

So why does Java require that we indicate what type of data each variable holds? Because it allows the computer to help you write correct programs and avoid errors. By keeping track of what type of data a variable holds, Java can help us make sure that certain operations on that variable are valid. This will make more sense as we go, but we can already see these checks at work in a simple case:

int i = 0;

Practice: Solve Practice with Primitive Types 2

Created By: Geoffrey Challen
/ Version: 2020.9.0

Write a snippet of code—not a function— that:

  • declares a variable digit of type char and initializes it to B
  • declares a variable named airTemperature of type double and initializes it to 78.8
  • declares a variable score of type int and initializes it to 99
  • declares a variable named semesterHasStarted of type boolean and initializes it to true

Representation in the Digital Age
Representation in the Digital Age

Examining the primitive types, you’ll notice that 6—byte, short, int, long, float, and double—are explicitly for storing numbers. What about boolean? Well, we represent false as 0 and true as 1.

That leaves char as the outlier that doesn’t seem to store a numeric value. But… it does! Here’s how:

0NUL
1SOH
2STX
3ETX
4EOT
5ENQ
6ACK
7BEL
8BS
9TAB
10LF
11VT
12FF
13CR
14SO
15SI
16DLE
17DC1
18DC2
19DC3
20DC4
21NAK
22SYN
23ETB
24CAN
25EM
26SUB
27ESC
28FS
29GS
30RS
31US
32SP
33!
34"
35#
36$
37%
38&
39'
40(
41)
42*
43+
44,
45-
46.
47/
480
491
502
513
524
535
546
557
568
579
58:
59;
60<
61=
62>
63?
64@
65A
66B
67C
68D
69E
70F
71G
72H
73I
74J
75K
76L
77M
78N
79O
80P
81Q
82R
83S
84T
85U
86V
87W
88X
89Y
90Z
91[
92\
93]
94^
95_
96`
97a
98b
99c
100d
101e
102f
103g
104h
105i
106j
107k
108l
109m
110n
111o
112p
113q
114r
115s
116t
117u
118v
119w
120x
121y
122z
123{
124|
125}
126~
127DEL

What is shown above is the mapping from numbers to character values. You can see that in action as you use the char type in Java:

char mystery = 56; // I can assign an int to a char... interesting
// Can you figure out what value this has? Try printing it...

You don’t have to take the table’s word for it. Java will show you either side of the mapping, and the only thing that decides which is the type you ask for:

char letter = 65; // legal, but please don't write this (see below)
System.out.println(letter); // 65, printed as a character
int number = 'A';
System.out.println(number); // 'A', printed as a number

Don’t initialize a char with a number in your own code. Write char letter = 'A', using a character literal like you did above. That says what you mean, and anyone reading it can see straight away which character you wanted. The example is here only to show you that the number was what Java had been storing all along.

The mapping above wasn’t sent down on stone tablets. It was agreed upon at the dawn of the computer age. And it leaves a lot to be desired, since there are many symbols in many alphabets that are not included in the table shown above!

But it makes an important point. Internally, computers store everything as a number. Any non-numeric data must be converted to numeric form—or digitized—before it can be manipulated by a computer.

We live in the digital age. You communicate increasingly through text that computers represent internally as numbers. You enjoy music delivered in a digital format and take photos with a digital camera. You can increasingly enjoy fine art from a distance due to high-resolution scans. One day soon you’ll have a completely digital medical record, consisting of medical information that was itself digitized so that it could be analyzed by a computer. Learning about computer science and programming will allow you to be a full participant in our digital present and future.

Course Design: Daily Lessons
Course Design: Daily Lessons

CS 124 is designed to give every student a chance to succeed. Below we explain one unusual aspect of the course. You can find more information on the course design document.

CS 124 does not hold lectures.

Study after study has found that lectures are an ineffective way to teach. Learning requires active engagement, not passive watching(1).

In CS 124, you’ll learn through active engagement with our interactive daily lessons. Each lesson mixes text, runnable examples, interactive code walkthroughs (unique to CS 124), and short videos. Along the way you have many chances to test your knowledge through practice problems, debugging challenges, and homework exercises, with course tutors supporting you every step of the way.

Daily lessons have many other advantages compared to lecturing. Returning to the content each day helps achieve spaced repetition, helping you learn even more efficiently. Students with different levels of prior experience can move at their own pace, which is impossible with one-speed-fits-nobody lecturing. This is particularly important in a course like CS 124, which enrolls students with a wide range of prior experience. Eliminating lecture space and time constraints has also allowed us to open up the course to anyone on campus.

We’ve also amassed a large and growing set of explanations from multiple instructors—Colleen and Geoff, and even some new faces from outside Illinois—and from course tutors. So if you don’t understand something, there’s probably another explanation that might help. We could never provide this kind of opportunity in a lecture-based format.

Code Quality: Linting with checkstyle
Code Quality: Linting with checkstyle

In addition to teaching you how to write correct code, we also want to teach you how to write good code: Code that’s beautiful and elegant and that you and those you work with will be proud of.

There are multiple aspects of code quality that we’ll be introducing throughout the semester. You may have already noticed one in our walkthroughs:

int i=0;
System.out.println(i);

The code above won’t run and print 0. Instead, something called checkstyle will complain about the formatting of the code, and encourage you to write it a bit differently.

For now, on the homework and practice problems these are just warnings. However, in the future, a small portion of the score on each problem will be for submitting code that is not just correct, but also high-quality. Let’s see an example of how that works using the practice problem from the previous lesson:

System.out.println( "Hello, world!" );

In each case, we’ll introduce an aspect of code quality first, warn you about it on the homework for a while to allow you to learn how to address it, and only then count it as part of your homework score.

Getting Help: The Forum
Getting Help: The Forum

CS 124 gives you five ways to get help when you’re stuck. Each day this week we introduce one of them. You can find them all on the getting help page.

Ask on the forum when the answer would help someone else too.

Our course forum is the nexus of course activity: announcements, questions, help, commiseration, and a certain amount of meme appreciation. Tutors and students are there day and night. Use it for anything that isn’t personal or sensitive, because answering it there helps every other student with the same question.

Two habits are worth building now.

Search before you ask. There’s a good chance your question has already been answered, which means you find out right now instead of waiting. It’s also good etiquette on any public forum you’ll ever use.

Ask so that you can be helped. “My code doesn’t work” is hard to answer. What you tried, what you expected, and what happened instead is easy to answer. The effort you spend making a question answerable comes back to you immediately.

One thing that isn’t optional: you’re responsible for what we post in the announcements category. That’s where deadline changes and quiz details show up.

More on the getting help page.

Homework Problem
Homework Problem

As a reminder, CS 124 allows students to collaborate freely on the homework problems. Please see the syllabus for more details.

Created By: Geoffrey Challen
/ Version: 2023.1.0

Write a snippet of code—not a function— that:

  • declares a variable goal of type char and initializes it to A
  • declares a variable years of type int and initializes it to 14
  • declares a variable named score of type double and initializes it to 98.8
  • declares a variable named januaryIsWarm of type boolean and initializes it to true

CS People: Ada Lovelace
CS People: Ada Lovelace

As we teach you the basics of computer science and programming, we’re also going to introduce you to computer science people—the humans behind technology’s remarkable achievements. Note that we include a few questions on each quiz on these short videos, so don’t skip them!

It may surprise you, but the first computer program was written long before there was a computer to run it! But everything about this person and their contributions to computer science is quite remarkable.

Women in Computer Science
Women in Computer Science

We’re Women in Computer Science (WCS)! Our mission is to support women, nonbinary individuals, and members of other underrepresented groups as they pursue interests and/or careers in technology. We host socials, technical workshops, corporate networking opportunities, and mentorship events to support students. You don’t have to be a woman NOR in computer science to join; you are welcome as long as you are welcoming! Stay connected with us at https://wcs.illinois.edu!

More Practice

Need more practice? Head over to the practice page.