Java for Beginners

My first program in Java

Last updated on March 3, 2019 by Michael Lossagk , 9 min  • 

1. Introduction


Many people have asked me if I can start a series that introduces programming step by step. That’s exactly what I want to do with this post. I will write a simple program in Java and present the steps. To understand this, no programming skills are necessary. What is needed is familiarity with the basic functioning of computers. This means you should be able to install programs and start commands on the command line.

2. Installation of Java


To be able to program with Java we first have to install the JDK. JDK stands for Java Development Kit and is a collection of tools to program with Java. I will go into technical details of the JDK in later posts. For the time being, it is enough for us to realize that we need the JDK to write and compile Java code. The JDK can be downloaded here or, depending on the system, can be installed as follows:

# macOS
brew install java # if homebrew is installed

# Linux
# Debian
apt-get install java-1.8.0-openjdk
# Red Hat Enterprise Linux (RHEL), CentOS, Fedora
yum install java-1.8.0-openjdk

# Windows
Go to oracle.com and install the latest JDK for Windows

After the installation we should be able to execute the commands javac and java on the command line:

# Java Compiler
javac

# The output should look something like this:
mick@mick-rMBP-2017 ~/Sourcecode/ $ javac
Usage: javac <options> <source files>
where possible options include:
  @<filename>                  Read options and filenames from file
  -Akey[=value]                Options to pass to annotation processors
...

and

# Java
java

# The output should look something like this:
mick@mick-rMBP-2017 ~/Sourcecode/ $ java
Usage: java [Optionen] <mainclass> [args...]
           (to execute a class)
   oder  java [Optionen] -jar <jarfile> [args...]
...

3. My first program in Java


Once the installation is complete, we can start writing our first program. For this we first create a text file with the file extension .java. In principle, this file can have any name, but it is good style that the name of the file is the same as the name of the class in the file. A class in Java is a surrounding construct that contains everything else, such as methods and variables. Java is an object-oriented programming language and simply said, each class corresponds to an object. I do not want to go too far at this point. We just remember: a .java file needs a class and it has the same name as the filename, but the case is case sensitive, that means HelloWorld == HelloWorld is correct, HelloWorld == HELLOWorld is wrong , Here’s an example:

// Dateiname: HelloWorld.java
class HelloWorld {}

We now have a file called HelloWorld.java and the above content. As you can see, a class in Java is simply created. We write a signal word or Keyword class and the desired class name behind it. Since a class is a surrounding element we still need the opening and closing curly braces {}. Currently the class is empty and our program would not work that way, so we need another function within the class, the main () function. We add the main function of the class by writing as follows:

class HelloWorld {
    public static void main(String[] args){}
}

This looks a bit more complicated, right? But that is not a problem. The main function is very important in Java and marks the entry point of a program. This means that whenever a Java program is started, the instructions in this function are executed first. You’re probably wondering what all the words mean before the word main and also within the brackets (). The words are keywords in Java and are called modifier (here public and static) and return values ​​of the function (in this case void). Inside the brackets are the type (String []) and the name of the function parameter (args). This information should not bother us at this point and I will go into separate posts on it separately. For the moment it is sufficient to know that without this information the program would not work, otherwise the main function could not be clearly identified and found. And as I have already written, the main function for launching a Java program is very important, so this information must be provided.

Similar to a class in Java, a function is an enclosing element. In this case, the main function is empty, so here again we write the opening and closing curly braces {}. The function may be empty and the program would work, as it is described in the example, but nothing would be printed to the console. But since we want to have something print to the console, we will complete the code as follows:

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

To display a text in Java on the screen we use the method println. This method expects a text as a parameter, which we will put into quotation marks (“”), in this case “Hello World!”. For Java to find the method we need to determine where to find it. In this case, we have to write System.out. before that. Simply put, we access internal Java libraries that provide us with everything so that we can produce an output on screen. Again, we should not think much about why exactly we have to write this way, but just accept that for the moment. This point is explained as well as the explanation of why we need modifiers in future posts.

4. Compile and Run


Our program is now finished and can be started. Well, almost. We must first convert the text file into machine-readable code. This is done with the help of a compiler. The JDK provides such a compiler with (javac) which we now use as follows:

First, we navigate on the command line to the directory in which our Java file HelloWorld.java is located. Then we execute the following statement:

javac HelloWorld.java

As a result we get a class file HelloWorld.class in the same directory as the text file HelloWorld.java. Class files are machine readable files and contain bytecode. This bytecode is only executable in a Java virtual machine (JVM). So that we can start the program now we have to open the file with the JVM. For this we enter the following command:

java HelloWorld

It should be noted here that we omit the ending .class in the above command. If everything works as expected we should see the following output on the screen:

Hello World!

If you have made it this far, then congratulations! You have just written your first program in Java and started it successfully.

Test it yourself online (if it does not work with Chrome, then allow third party cookies or use a different browser):

The attentive reader notices that in the example of https://repl.it the main class is Main.java instead of HelloWorld.java. It is specified in this tool that the main method must be in the Main class. However, this should not bother us at this point. Play around a bit and modify your code accordingly to get both versions up and running.

5. Questions


You have now learned how to write a program in Java and should be able to answer the following questions:

  • What is the JDK and what does the abbreviation stand for?
  • What is the JVM and what does the abbreviation stand for?
  • How are JDK and JVM different?
  • What is a .class file?
  • What is a class in Java?
  • Why do I need main () function?
  • How do I compile a file into machine-readable code?
  • What exactly is bytecode?

6. Summary


In this tutorial, I showed how to write a simple program in Java. It was shown how a Java class has to look like and how the rough structure is. Then we compiled and executed the file.

The appropriate code examples can be found in my GitLab repository. If you want to learn more about Java and programming I can recommend you this book.


Michael Lossagk
Michael Lossagk
Coding Enthusiast
Founder @ TechDiffuse

You may also like


Share this: