Constructs and Compilation
> Click or hit Control-Enter to run the code above
Q3 Review: return
Type
double[] getAllValues(double[] values, int count) {
What is the return type of the function declared above?
Q3 Review: break
int counter = 0;
for (int index = 0; index < 10; index++) {
counter++;
if (index > 5) {
break;
}
}
What will the value of counter
be when the code above finishes executing?
The do-while Loop
Remember the while
loop?
while (condition) {
// do something
}
It also has a friend:
do {
// do something
} while (condition);
What’s the difference?
> Click or hit Control-Enter to run the code above
Switch Statements
Remember the if
statement?
if (conditionA) {
// do something
} else if (conditionB) {
// do something else
} else if (conditionC) {
// ...
}
It too has a friend:
switch (variable) {
case A:
// do something
case B:
// do something else...
}
Switch Statements
switch
is more limited than if-else
, but also more flexible:
-
It only works for testing primitive types and
String
s -
But you can match multiple
case
s in a singleswitch
statement -
Execution starts at a matching
case
statement and continues until abreak
statement is reached
> Click or hit Control-Enter to run the code above
Questions on Imperative Programming?
Compilation and Execution
(And now, a small digression.)
So far we’ve been letting you admire the magic that happens when you execute Java code. But what is actually happening?
-
As a computer scientist, part of your job is to be curious about how things work!
-
This will also help clear up a few things that may have been confusing to you up until this point…
-
It’s also quite interesting…
-
…but note that I am not going to go into detail here 1. Ask on the forum if you want to know more!
When You Run Java Code
Roughly speaking, when you run your Java code—on our playground, on PrairieLearn, or in IntelliJ—the process works like this:
-
Compilation: your Java source code is compiled to a simpler representation called Java bytecode by the Java compiler (
javac
).-
Errors that occur at this stage are called compilation errors.
-
If your code doesn’t compile, it doesn’t run.
-
-
Execution: the bytecode from Step 1 is then executed by a program called the Java Virtual Machine (JVM) (
java
).-
Errors that occur at this stage are called runtime errors.
-
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run the code above
Compiler Errors v. Runtime Errors
Java and many languages that followed it have tried to transform runtime errors into compiler errors. Why?
-
You compile your code before it runs: and so before you have to demo it to a client, or before you deploy it to hundreds of users.
-
Catching errors at this stage is critical.
> Click or hit Control-Enter to run the code above
Java Compilation: javac
The Java compiler transforms Java source code into Java bytecode.
On many machines it is a program called javac
.
(Quick demo.)
Java Execution: java
The Java Virtual Machine executes Java bytecode.
On many machines it is a program simply called java
.
(Quick demo.)
Bytecode v. Machine Code
-
Many compiled languages compile into instructions that an actual CPU or processor can execute—we call this machine code.
-
Java does not—it compiles into bytecode that is then run by the Java Virtual Machine (JVM), a piece of software.
-
Why?
Write Once, Run Anywhere ™
(I will do my best to avoid a truly Wallacian stem-winder of a digression here, since this is a truly fascinating story.)
-
Different computer processors use different instructions—this is still true today, but was even more true when Java was being designed
-
(This is kind of like them speaking different languages.)
-
-
So if I take a program that was compiled for an
x86
processor and try to run it on anARM
processor, it won’t work -
But if I take a Java program that was compiled into Java bytecode I can run it anywhere…
-
…as long as that computer has the Java Virtual Machine (JVM).
-
(This is the difference between the Java Runtime Environment (JRE) and the Software Development Kit (SDK). The SDK includes
javac
, the JRE does not.)
As Computers Get Faster…
compilers also get faster, leading to important and useful changes in language design.
// Wait, what black magic is this?
var integerValue = 5;
-
Java 10 introduced local variable type inference
-
Which means that you do not need to specify the type for local variables if the compiler can determine what it should be
Java 10 Type Inference
You can use this feature now using IntelliJ and on our PrairieLearn homework problems—just not in the slide playground 2.
var sum = 0;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
Let’s Talk main
public class Example {
public static void main(String[] unused) {
System.out.println("How did I get here?");
}
}
You’ve been using and even writing main
methods?
But what is it?
Everything Has To Start Somewhere
public class Example {
public static void main(String[] unused) {
System.out.println("And this is it!");
}
}
-
When Java runs your program, execution has to start somewhere.
-
If a class has a
main
method Java can execute that class and begin the process of running a program. -
So every Java application starts in
main
somewhere.
But What About unused
?
public class Example {
public static void main(String[] unused) {
System.out.println("And this is it!");
}
}
But why is main
passed an array of Strings
s (that we sometimes ignore)?
Announcements
-
Many lecture videos have been posted to YouTube—and we’re working on the rest
-
Friday we will begin discussing objects, next Monday will be midterm review
-
Office hours for MP2 continue—please get it done, since it will help you prepare for the midterm exam next week
-
Next week we will hold our first midterm exam: a one-hour quiz worth 5% of your grade that cannot be dropped
-
A new MP will not be released Monday to give you a week to finish the midterm