1. Introduction to the Java Ecosystem

Many newcomers get caught in "tutorial hell": they read a lot, watch a lot of videos. However, it is important to take the first step, and that is why this chapter is intended to help you lose your reluctance to do practical exercises. In the process, we will get to know our development environment better, be able to run programs from the command line, and see more clearly the division of tasks between the Java compiler and the runtime environment.

Prerequisites

  • Understand the task of Java compiler, bytecode and JVM

  • Be able to compile Java programs on the command line

  • Know the difference between java and javac programs

  • Set up and be able to operate Java development environment in a basic way

1.1. Bytecode and JVM

In the early days of Java, the usual way for compilers was to generate a directly executable machine file. Sun Microsystems wanted something different: a platform independent programming language - this led to Java. To achieve this, the compiler is no longer allowed to generate machine code, but the Java compiler generates bytecode. This bytecode is not bound to a machine. So that the bytecode can be executed, a runtime environment and a function library must exist, called Java Runtime Environment (short JRE). Part of the JRE is the Java Virtual Machine, short JVM, which executes the bytecode.

1.1.1. Porting Java programs ⭐

Anyone starting out in programming is bound to have come across a classic: the Hello World output. In the mid-1970s, this small example appeared in a C tutorial and has since been ported to various programming languages. Although the program is small, it serves an important purpose: to test whether all the development tools are installed and working correctly.

Task:

Save the following program named Application.java. Pay attention to the upper/lower case.

Application.java
public class Application {
  public static void main( String[] args ) {
    System.out.println( "Aye Captain!" );
  }
}

We can now start the program with

$ javac Application.java

and compile it with

$ java Application

execute.

Question:

  • Is it possible to copy the file Application.class from a Windows computer to Linux and make it run there using the java program?

  • What software must be installed on the computer?

1.2. Tools for Java developers

The choice of development environment often depends on personal taste, they all handle the core tasks well:

This workbook is completely independent of the IDE. All developers should work intensively with the shortcuts, the debugger and the other tools. The web pages of the developers and YouTube offer plentifully material for it, a small selection:

1.2.1. Get to know error messages of the IDE ⭐

This task is about getting to know the development environment a little better.

Let’s take the following program again:

Application.java
public class Application {
  public static void main( String[] args ) {
    System.out.println( "Aye Captain!" );
  }
}

Task:

  • Transfer Application.java to the IDE.

  • Deliberately build errors into the program code, and observe the error messages. Some suggestions:

    • Change the file name.

    • Change the case, for example write Class instead of class.

    • A main program is not started until the class has a special method public static void main(String[] args). What happens if the method is not called main, for example, but something else, e.g. Main or run?

    • Try to output greek letters or hearts, can you do that?

    • White space is often used, so spaces (usually not a tab) and line breaks appear after each statement. Is the following valid like this?

      public class Application{public static void main(String[]args){System.out.println("Aye Captain!");}}.

1.3. Suggested solutions

1.3.1. Porting Java programs ⭐

If the Application.class file is copied from a Windows machine to a Linux machine, it will run there as well. To run the file, a runtime environment like Oracle JDK is always required.

1.3.2. Get to know error messages of the IDE ⭐

You could deliberately include the following errors:

  • If the class is public, the filename must be the same as the class. So we could change the filename or classname. The IDE recognizes the error and suggests a renaming.

  • Keywords are always lowercase in Java. What happens if you capitalize a keyword like public or class or capitalize single letters? In that case there is a compiler error.

  • If the start method is not declared as public static void main(String[] args), but differently, the editor can not guess, but the compiler will translate the program correctly, because public static void run(String[] args) could in general be a method we want. It is a semantic error and not a syntactic error, which the editor or the compiler cannot notice. But the runtime environment will throw an error that the main(…​) method is not present when the program is to be started. (semantic error

  • A method consists of a sequence of statements — these statements are in a block enclosed in curly braces. The same is true for the class, which consists of a collection of methods that must also be placed in a block altogether. Indentations do not matter in Java. You can provoke an error by omitting the curly braces completely.

  • Every open curly bracket requires a closing curly bracket. If you omit a curly bracket, it will cause a compiler error.

  • String literals are enclosed with different symbols in different programming languages. Some programming languages use double quotes ("), other programming languages use a single quote ('), some programming languages use backticks (`). Java can basically accept the strings only in double quotes, otherwise there will be compiler errors. Single quotes are used for single characters (data type char).

  • In Java, white space is used to make the source code clearer, especially to make the blocks visible. You can save some white space. You can try to find out which of the whitespace characters you are allowed to delete and which you are not. The code given in the task is basically fine.