← Back to course: Java from scratch

jshell, errors and week 1 review

Video coming soon

We close the first week with three things: a tool to experiment without writing classes (jshell), a map of the errors you will see every day, and a review of what you learned. Knowing how to read an error is as important a skill as knowing how to write code: experienced programmers do not make fewer mistakes, they just understand them faster.

jshell: Java as a calculator

The JDK includes jshell, an interactive interpreter. You open it by typing jshell in the terminal. You type an expression, press Enter and see the result instantly; you need no class, no main and no final semicolon. It is perfect for testing a quick doubt, such as what a division returns or how a String method behaves. To leave, type /exit.

text
jshell> int x = 5
x ==> 5

jshell> x * 3
$2 ==> 15

jshell> var s = "hi"
s ==> "hi"

jshell> s.toUpperCase()
$4 ==> "HI"

jshell> Math.sqrt(16)
$5 ==> 4.0

jshell> /exit

When an expression is not stored in a variable, jshell gives it an automatic name such as $2 or $4, which you can reuse. Use jshell to explore, but remember that real programs live in .java files: that is where you will keep and share your work.

Three families of errors

  • Compile errors: the code breaks the rules of the language (missing semicolon, wrong type, undeclared variable). The program does not even start.
  • Runtime errors (exceptions): the code is valid but something fails while it runs, such as dividing integers by zero or reading a position that does not exist.
  • Logic errors: the program runs without complaint but gives a wrong result. They are the hardest because Java cannot detect them.
java
public class Main {
    public static void main(String[] args) {
        int apples = 10;
        int people = 0;
        System.out.println("Start");
        int each = apples / people; // runtime error here
        System.out.println("Each gets " + each);
    }
}
// Real output:
// Start
// Exception in thread "main" java.lang.ArithmeticException: / by zero
//     at Main.main(Main.java:6)

Look at the message above. First comes the exception type (ArithmeticException), then the cause (/ by zero), and finally the call stack: the line Main.java:6 tells you exactly where it happened. You read it from top to bottom, and the most useful part is usually in the first lines that belong to your code. With a compile error something similar happens: javac shows the file, the line and an arrow under the problem spot.

java
public class Main {
    public static void main(String[] args) {
        int celsius = 25;
        // Logic error: integer division loses the fraction
        double wrong = celsius * 9 / 5 + 32;      // 77.0 (ok here by luck)
        double fahrenheit1 = 9 / 5 * celsius + 32; // 9 / 5 is 1, not 1.8
        double fahrenheit2 = 9.0 / 5 * celsius + 32;
        System.out.println(wrong);
        System.out.println(fahrenheit1);
        System.out.println(fahrenheit2);
    }
}

The second program gives no error and still fails: 9 / 5 with integers is 1, so fahrenheit1 comes out as 57.0 instead of 77.0. It is a typical logic error. The defense is to test with values whose result you know and to write 9.0 when you want decimals. Summary of the week: you installed the JDK, compiled with javac and ran with java, you know the types int, double, boolean, char and String, you can declare with var and final, and you can tell the three kinds of error apart.

Test what you learned

Question 1 of 6

What does the JDK do that the JRE does not?

✎ My notes

Saved only in this browser. Sign in to keep it in your account.0/5000 characters

← PreviousNext →