KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • map-reduce-filter : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Variables and Types

var i = 0
println(i)
var c = 'A'
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 Kotlin distinguishes between different types of data.

What Computers Are Good At
What Computers Are Good At

Computers represent the most powerful tool that 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 capabilties:

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 Kotlin program:

var i: Int = 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 tells the computer that the value of i may change (var)
  4. 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.

var i: Int = 0
println(i)

Declaration and Initialization
Declaration and Initialization

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

var i: Int // I'm going to use a variable named i to store integers
i = 0 // I'm setting its initial value to zero
println(i)

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

var i: Int
// Excuse me... what's the initial value of i?
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.

var i: Int = 0
println(i)

Type Inference
Type Inference

When we declare and initialize our variable in the same statement, Kotlin does not require that we explicitly provide a type. For example, this:

var i: Int = 0

is equivalent to

var i = 0

In the second case, Kotlin can infer the type of i from the value that we use to initialize it: 0, an Int, literal. Because this is pretty convenient, we’ll normally omit the unnecessary type notation when we can. There are times when explicit type notation is needed, and we’ll discuss them when we get there.

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.

var i = 10 // 10 is an Int literal, i is a variable
var threshold = 0.5 // 0.5 is a Double literal, threshold is a variable
var isACat = true // true is a Boolean literal, isACat is a variable
var favorite = '8' // 8 is a Char literal, favorite is a variable

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

var favorite: Char = "8"

But be careful here, since Kotlin’s type inference might “help” you in a way that you don’t expect!

// notAChar will have its type inferred as String, another Kotlin type we'll learn about soon!
var notAChar = "8"

Types
Types

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

Here are all 8 Kotlin basic 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

var bigValue = 88888888
println(bigValue)

Kotlin has four different basic 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 Kotlin 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

var temperature = 82.4
println(temperature)

Kotlin 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

var spring2024Rules = true
println(spring2024Rules)

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 Kotlin’s Boolean type can store only two values: true and false.

Character Values
Character Values

var catsInitial = 'X'
println(catsInitial)

Finally, Kotlin provides the Char type for storing a single character value.

Practice: Practice with Primitive Types

Created By: Geoffrey Challen
/ Version: 2020.6.0

Internally, computers represent everything as a number. But they can still store and process all kinds of information—from simple numbers to photos to music to the entire human genome. You'll learn more about how they can do that as you go along.

In Kotlin, as long as we provide an initial value when we declare a variable the compiler can usually guess—or infer—what the type is. The variable still has a type, and Kotlin will make sure that we don't change its type later. Assigning a type to each variable help us write more correct programs. For example, it doesn't make sense to try and add a boolean value (true or false) to an integer value (1, 2, etc.). But, unlike some other languages, in Kotlin we don't always need to provide an explicit type.

Let's get practice working with some of Kotlin's simplest or primitive data types. Write a snippet of code—not a function— that:

  • declares a variable (var) 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 Kotlin enforce rules about 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, Kotlin 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:

var i = 0

Practice: 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 and initializes it to the character 'B'
  • declares a variable named airTemperature and initializes it to 78.8
  • declares a variable score and initializes it to 99
  • declares a variable named semesterHasStarted 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:

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 Kotlin:

var mystery = 56.toChar() // I can convert an Int to a Char... interesting
// Can you figure out what value this has? Try printing it...

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!)

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 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 syllabus.

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 staff 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-all 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 staff. So if you don’t understand something, there’s probably another explanation that might help.

Homework Problem
Homework Problem

As a reminder, CS 124 now 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 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 on! But everything about this person and their contributions to computer science is quite remarkable.

More Practice

Need more practice? Head over to the practice page.