Compilation and Type Inference

Created By: Geoffrey Challen
/ Updated: 2022-09-15

Welcome back to CS 124! You are not alone.

This lesson is a lot of fun. 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. Then we'll discuss Java type inference.

Warm Up Debugging Challenge

Let's warm up with another graded debugging challenge!

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:

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

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.


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.

Discuss compilation. Use examples, probably at the command line. Point out some errors that can occur. Emphasize that these errors occur during development.


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

Discuss execution. Use examples, probably at the command line. Point out some errors that can occur. Emphasize that these errors occur in 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:

  • Development: the phase of software development where the programmers are writing and testing new code.
  • Production: the point at which the software program has been released to its actual users

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.

Discuss the difference between production and development. Best to use an example of your production and development environments!

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:

Show how to complete the homework problem above. Feel free to cover multiple approaches!

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:

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:

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:

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:

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 use val. Show both initialization and based on function return type.

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

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Script Parser Warm-Up

Next let's discuss how to approach our next practice homework problem. This problem is a bit trickier, since we need to determine when to properly insert entries into our Map, and do some String parsing. So let's discuss how to get started.

Please don't attempt this practice problem until you are done with yesterday's homework problem!

Discuss the Script Parser homework problem, both how to parse the String, and how to only add to the Map when needed.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

No New Homework Today

Please continue to work on yesterday's more challenging problem. Good luck! Jump on the help 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.