KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Testing and Compilation : 09/25/2026

  • Practice with Collections : 09/24/2026

  • Maps and Sets : 09/23/2026

  • Lists and Type Parameters : 09/22/2026

  • Imports and Libraries : 09/21/2026

  • Multidimensional Arrays : 09/18/2026

  • Practice with Strings : 09/17/2026

  • null : 09/16/2026

  • Algorithms and Strings : 09/15/2026

  • Strings : 09/14/2026

  • Functions and Algorithms : 09/11/2026

  • Practice with Functions : 09/10/2026

  • More About Functions : 09/09/2026

  • Errors and Debugging : 09/08/2026

  • Functions : 09/04/2026

  • Practice with Loops and Algorithms : 09/03/2026

  • Algorithms : 09/02/2026

  • Loops : 09/01/2026

  • Arrays : 08/31/2026

  • Compound Conditionals : 08/28/2026

  • Conditional Expressions and Statements : 08/27/2026

  • Operations on Variables : 08/26/2026

  • Variables and Types : 08/25/2026

  • Welcome to CS 124 : 08/24/2026

Testing and Compilation

var i = 1;
var j = "1";

Welcome back to CS 124! You are not alone.

This lesson is a lot of fun. First, we’ll introduce you to the idea of writing tests, which is somewhat we’ll continue to train you on for the rest of the semester. Next we’ll spend some time on our first discussion of Java internals. Specifically, we’ll explain more about what happens when your code is run. Finally, we’ll discuss Java type inference.

Writing Tests
Writing Tests

On upcoming lessons you’ll encounter a new kind of homework problem. Instead of providing a solution, you’ll be challenged to write tests. For your tests to pass, they must accurately distinguish between incorrect and correct submissions. (Note that we will not include this kind of problem on quizzes this semester.)

Why are we teaching you to write tests? For several reasons. First, writing tests is a very common part of software development. Usually when you contribute code to a project, you’re not just expected to provide the code that implements a new feature, but also the tests that ensure that your code works correctly. Writing tests also ensures that you understand the problem description, in a more direct way than providing a solution.

But the most important reason we’re teaching you to write tests is that it trains you to think defensively about mistakes that you might make while solving the problem. Authoring an accurate test suite requires enumerating all the things that a programmer—such as yourself—might get wrong:

Test suites train that little voice in your head that can guide you away from common errors. Train it well, and you’ll avoid common pitfalls, make fewer mistakes, and spend less time debugging.

What’s a Test?
What’s a Test?

We’re going to work our way into writing tests incrementally. We’ll start by introducing some terminology to define what we mean by a test and by a test case. We’ll also explain the relationship between the method being tested and the test itself.

int addOne(int value) {
return value + 1;
}

Practice with Test Correctness
Practice with Test Correctness

Next, let’s discuss how to write a test for a simple method that we’ve completed before.

// Testing the addOne() method
void test() {
return;
}

Practice: Test Array All Multiples
Practice: Test Array All Multiples

Now let’s complete a practice test correctness problem.

Created By: Geoffrey Challen
/ Version: 2022.8.0

Write a method named arrayAllMultiples that, given an array of int values greater than 0, returns whether the values are all integer multiples of one of the values in the array, which we'll call the base. For example, given the array {4, 2, 8} you would return true, since 4 and 8 are integer multiples of the base 2. Given the array {2, 2, 5} you would return false, since there is no value that all others are integer multiples of. (If the array contains 1, you should always return true, but there's no need to handle this case specifically.)

You should approach this problem in two steps. First, identify the base—the value in the array that you are going to check whether others are multiples of. This may sound complicated, but it's straightforward. You do not need to and should not check whether every value is the base! Your solution should not contain a nested loop.

Next, check all the values in the array and look for a counterexample—a value that is not a multiple of the base.

The passed array may be null or empty. In those cases you should return false. Valid arrays will contain only integers strictly greater than 0.

Test Correctness Challenge

Your challenge is to write tests for the problem described above.

  • Provide a method named test that accepts no arguments and does not return a value.
  • If the implementation of the method described above is incorrect, your test method should fail an assertion.
  • If it is correct, do not fail an assertion.
  • You will want to use Java's assert method
  • Your test code should not use loops or define extra methods.

Compilation and Execution
Compilation and Execution

Our focus in this class is on teaching you to think computationally and express those thoughts coherently in the Java programming language. Once you do that, you’ll be able to learn other programming languages easily and accomplish other computational tasks.

So we don’t spend a lot of time on exactly what is going on under the hood when Java executes the code that you submit. On some level, it doesn’t matter. As long as the core language constructs have a stable meaning, the internals of how things work don’t matter. When I write:

for (int i = 0; i < 8; i++) {
System.out.println("Happy birthday!");
}

I expect to see a message displayed 8 times. Java could and might completely change exactly how that is accomplished, but as long as it is accomplished, I’m not going to notice. This is a powerful idea in computer science, even if at times the methods of implementation are somewhat less than practical.

But! In this case having a bit of an understanding of what is happening is actually useful. And, what is actually happening is quite interesting! So let’s peek under the hood…

Two Steps
Two Steps

When you run a Java program there are actually two distinct steps that are taking place:

  1. Compilation: in this step your Java source code is compiled or translated into a different set of simpler instructions. The compiler can and does catch certain kinds of errors at this stage.
  2. Execution: the Java compiler produces a file containing bytecode, which can then be executed. This is when you program actually runs and does whatever you’ve instructed it to do. There are other errors that can occur at this stage.

Compilation
Compilation

Let’s talk a bit about when compilation happens, what is produced, and the kind of errors that the compiler can produce during this step.

Execution
Execution

Now let’s return to the same environment and discuss the execution step and the kind of errors that can occur at that point.

Development v. Production
Development v. Production

One of the reasons understanding these steps is so important is that they have a relationship to the difference between development and production:

Compiler errors generally only happen in development. That means that only developers see them! In contrast, runtime errors can happen in production! That means they might affect actual users.

Software developers acquire a high degree of patience with broken and crashy software. Users do not. That means that, if you can reduce runtime errors and catch them during development, you will produce better software. A new generation of compilers aim to do just that. Which would prevent you being able to do this, which always strikes me as insanely dumb:

// WHY DOES JAVA LET ME DO THIS!?!?!?!
String s = null;
System.out.println(s.length());

Practice: Solve List to Set

Created By: Geoffrey Challen
/ Version: 2021.9.0

Given a List of Strings, write a method toSet that converts the List to a Set of Strings that contains all the same Strings in the original list. So given a List containing "test", "me", and "test", you would return a Set containing "test" and "me". assert that the passed List is not null.

Java Type Inference
Java Type Inference

Before we conclude, let’s look at a new Java feature: type inference.

Let’s return to the very beginning of our experiences with Java. As a reminder, Java requires that we provide a type when we declare a variable:

int i;

This is a variable named i that can store values of type int. We know this.

But it’s more common to both declare and initialize a variable in the same statement:

int i = 0;

This is a variable named i that can store values of type int that we initialize to 0. We know this too.

But let’s look more carefully at this common and seemingly innocuous statement:

int i = 0;

Notice something interesting? We’ve actually told Java the type of the variable i twice. Once through the declaration, but a second time through initialization. Let’s split them apart:

int i; // Type named via declaration
i = 0; // Type indicated via initialization

So the Java compiler could determine the type based on how we initialize the variable. This is known as type inference, since the compiler infers the variable type based on how it is used. (Assignment is just one example of usage. We’ll look at another.)

The Java compiler is not particularly sophisticated. But, since Java version 10, it can infer types in certain situations. Here’s how to enable that:

// Show how to enable Java type inference

Pretty cool! Note that this limited form of type inference is available in our playgounds, and for you to use on your homework problems. (It may not work when we get to the Android project, because Android is using an older version of Java.)

No New Homework Today
No New Homework Today

Please continue to work on the more challenging problem from the previous lesson. Good luck! Jump on the tutoring site or find us on the forum when you need some assistance. And keep in mind that this is just one problem…

More Practice

Need more practice? Head over to the practice page.