Java Jumpstart - Part 2: Writing, Compiling, and Running Your First Program

ยท

5 min read

Java Jumpstart - Part 2: Writing, Compiling, and Running Your First Program

Now we have installed all the necessary tool for a seamless Java development. It's time to go more in depth to understand how Java works.

Understanding the Execution Process of Java Code?

When working with Java code (saved in a file with the .java extension), the initial step involves compiling it using the Java compiler, javac. This compiler, which is included in the JDK installed during Part 1 of this series, transforms our code into bytecode, stored in a .class file. Subsequently, the Java Virtual Machine (JVM) interprets this bytecode, converting it into machine code compatible with the underlying operating system (OS) and system architecture.

The JVM assumes a pivotal role in achieving platform independence, even though it is inherently platform-dependent. This means that we have the flexibility to write and compile our code on any platform, with the assurance that it will run seamlessly on any other device equipped with the JVM. The image below illustrates a simplified sequence of the execution flow.

Note: JDK contains JRE which in turn contains JVM as illustrated in the image below.

Our first Java code

Nice. Now we have a better understanding of how our codes are executed.

Lets see this in action by writing our first Java code.

Like I said in Part 1, feel free to use any IDE you prefer but I will be using IntelliJ IDE. So first of all we will create a new project. Click on the "New Project" button.

Now enter the Project Name, select "Java" under "language", then make sure that the version of the JDK installed is selected. Now click on "create" to create your project.

Now, you should have a view similar to the one in the image below. IntelliJ pre-writes some codes for us. When we click the play button, IntelliJ helps run the commands that compile and execute our codes. That's why we get "Hello and welcome!" on the terminal.

What does IntelliJ do under the hood?

To see what IntelliJ does for us behind the scenes, right click on Main.java and click on open in terminal

In the opened terminal, type: javac Main.java and press enter. This command compiles the code to bytecode and saves it as "Main.class". You should already see the file in the src directory. Now, type: java Main and press enter to execute the generated bytecode. Remember not to include the .class extension when running this command: java Main . Now you should see "Hello and welcome!" on the terminal. Great!

Note: IntelliJ saves its compiled file in "out" folder as shown below:

Anatomy of a Java Project

In Java, by convention, we name our base package in reverse domain order to avoid name conflicts with packages from other projects. This does not mean you need a domain name to build Java projects. For instance, we want to build a book management system, our project could be structured like this:

/project
|-- src
|   |-- com
|       |-- example
|           |-- bookmanagement
|               |-- model
|                   |-- Book.java
|               |-- service
|                   |-- BookService.java
|               |-- repository
|                   |-- BookRepository.java
|               |-- Main.java

The base package will be com.example.bookmanagement".
Then, we have sub packages like model, service and repository.

We use packages to group related classes. This helps us organize our codebase. Classes serve as containers for related methods or functions. Classes are named in "PascalCase". The name of a class should match its file name exactly. So in the above example, we have the following classes: Book, BookService, BookRepository and Main in the following files: Book.java, BookService.java, BookRepository.java and Main.java respectively.

A function or method is the smallest building block of a project. It can be likened to a recipe. It takes specific ingredients (input parameters), performs a set of instructions (code statements), and produces a dish (output or result). A function should always have a return type for instance int, String, boolean when it is returning something and void when it is not. This felt unusual to me at first, coming from a dynamically typed language. However, over time, I came to appreciate its benefits. If you're in a similar situation, you might find it surprisingly valuable as well. In the block of code below, we have two methods; main() with return type void and calculate() with return type int . We will discuss more on these signatures in the coming parts.

public class YourClassName {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

    public static int calculate() {
        return 3+4;
    }
}

Note: In a Java program, it is a requirement to have at least one class that contains the main method since Java is an Object-Oriented language. The main method serves as the entry point for the Java Virtual Machine (JVM) when executing a Java application. The main method must adhere to the following signature:

public class YourClassName {
    public static void main(String[] args) {
        // write your code here
    }
}

We will look at the reason why the main method must adhere to this signature. For now just know that String[] args allows us pass in command-line arguments into our program.

Also note that in the main method can belong to any class in your program, not necessarily a class named Main . What matters is that the main method has the correct signature and is properly defined within a class.

Fantastic! Together, we've laid a good foundation, and there's so much more to explore. I hope you're as excited as I am about the journey ahead. Stay tuned for the next part where we'll dive into Types in Java. Your feedback and questions are always welcome. Happy coding! ๐Ÿš€

ย