How do I make programs out of Java

Your first Java program in 15 minutes

0

Would you like to learn Java, but have no experience with the programming language? In this beginner's tutorial you will learn how to create your first Java program in 10 minutes. You will already be writing and executing your first lines of code.

In order to get started right away, a few requirements must be met. Let's start.

1. Install the Java JDK

To be able to program with Java, you need the Java JDK (Java Development Kit).

It contains all the applications and tools that dz needs to program with Java.

If you have the Java JDK haven't installed yet, then check out our "Download and Install Java" article. Here we explain step by step what the JDK is and how to install it.

2. Text editor or IDE

In addition to the Java JDK, you only need a text editor.

Windows Notepad is sufficient here. However, I recommend Notepad ++ (free). Other text editors or the popular Visual Studio Code Editor can also be used for Java.

Note: Later in your "Java career" you will likely want to switch to a more professional Java IDE such as Eclipse.

3. Save your first Java file

At the beginning we open any text editor such as Notepad or Notepad ++, or similar.

As soon as you have opened a text editor, please paste the following code into the editor (copy / paste). Please do not scare!

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

If you're new to programming, the lines of code shown above likely seem like high-level math.

It is completely normal.

No Java novice started out with a complete understanding of all commands, keywords, and concepts. We start step-by-step.

If you still want an overview of what you have inserted into your editor, read the following "optional part":

Optional part: What did I add there? (if you don't care, skip the bullet points below)

  • You need a "class": With class [class identifier] you can create a class. The class identifier must not contain any spaces or special characters.
  • This is followed by an opening and a closing curly bracket {}
  • You can define methods within these brackets. In methods you write code that is to be executed when this method is called.
  • In the code above you will find a method called "main". A main method is a method that is executed first when a program is started.
  • In Java, the "head" of the method must always be structured as above: public static void main (String args []), followed by curly brackets again

Go on!

Find the following code in your editor: System.out.println ("Hello World!");

This is the first command your computer understands. With system.out.println we can output texts. We write the text that we want to display in "Hello World!" In round brackets. (Texts must be in quotation marks in Java).

The semicolon at the end of the line is also important. With this, Java knows that this instruction ends, since instructions can also be written over several lines.

4. Save the Java file

Now we need to save your new file. Like any file, Java code has a own file extension. Click on "Save As ..." (or "Save"), and save your file with the extension .Java e.g. MyFirstProgram.java

Pay attention to the file extension .java:

5. Optional part: How is Java code compiled?

(This is an optional part if you are interested in what actually happens in your computer when Java code is executed. If this part does not interest you, skip to the next heading.)

Before you can run your code, we need to compile it. You have to imagine that your computer only understands ones (1) and zeros (0). Our computer cannot understand our Java code at all.

Fortunately, the Java JDK provides a program, namely the "Compiler". This translates our Java code into machine language (zeros and ones).

What you see in the picture above is only half the story.

Because Java has the advantage that it not only runs on a certain operating system, but on several (Windows, MacOS, Linux, Android, etc.).

Accordingly, the machine code must also be tailored to the respective operating system.

Every computer that wants to run a Java application must install the Java JRE (Java Runtime Environment). This includes a so-called for the respective operating system Just-In-Time Compiler.

Our compiler converts Java code into Java bytecode um, which can then be executed by the JIT compiler for the respective operating system.

The process looks like this:

6. Start your first Java program

The compilation program is called "javac". First we open our command prompt:

Now we navigate within the command prompt to the folder in which the Java file we have just created is saved.

We can do this with the CD Make command (change directory):

(My file "MeinErstesProgramm.java" is in the folder C: \ Users \ Tarik \ Desktop \ MeinErstesProgramm)

If you have followed our article "Install Java JDK", then you can already use the javac command.

  • Otherwise you will have to find the folder in which you installed the JDK.
  • Go to the "bin folder" and copy the entire path (as well as \ javac.exe) into the command prompt.
  • This is followed by a space with the filename of your .java file.

Path to javac.exe

Please note that the path might be different with you!

Again, make sure to use quotation marks for the path, otherwise you may get an error:

Command to run your first Java program

If we now look at our folder, we see one HelloWorld.class (HelloWorld was our class name in the code).

After compiling a new file appears with the extension .class (HelloWorld.class)

You have just received a compiled Java bytecode that can be executed with the "java" command in the command prompt. Proceed as follows:

  • Now give the again Path of your JDK installation
  • as \ java.exe and
  • the class name HelloWorld into your command prompt.

If you have a look at the following screenshot, the above task will be much easier to understand:

Path to java.exe and execution of the Java bytecode "HelloWorld"

As soon as you confirm the command, you should see the text "Hello World!" in your command prompt. If you came here, you deserve my respect!

Your challenge: change the text

Finally, a little challenge: Can you manage to output several texts of your choice? You can command System.out.println use several times. You can find a possible solution below.

Possible solution:

class HelloWorld {public static void main (String args []) {System.out.println ("Hello ..."); System.out.println ("at ..."); System.out.println ("LerneProgrammieren ..."); System.out.println ("I ..."); System.out.println ("learn .."); System.out.println ("Java."); }}

Conclusion on your first Java program

If you've done everything so far, then you've made a big step in the direction of programming.

You don't need to understand all of the keywords and concepts in this article. Experiment, visit our YouTube channel, and check out more Java tutorials on Learn to Code to Understand Java Better.