How do I do Java

Introduction to the basics of Java

In this post, we'll take you into the Java basics so that you get a good overview.

You want that quickly Java basics understand? Then check out ours Video to do this!

Programming in Java - What are Java classes?

As an introduction to the Java basics you can see a simple program code here:

This starts with. One or also class called, is a section of a program. Depending on the size of the program, there can be several Classes give. For a simple “Hello World!” Program, however, one is enough Java class out. So that the Compiler knows where in the Java code a class begins and ends, curly brackets are used at the beginning and at the end. Below we explain what a Compiler is and what he does.

It's up to you like that Java class This means, however, that you have to adhere to the following rules: On the one hand, the name is either made up of letters or letters and digits, whereby the first character must be a letter. On the other hand, the name must not contain any spaces or special characters.

Structure of the syntax

Important for the Java basics is above all that construction or the grammar one Java program to get to know. One speaks in this context of the Syntax. You already have something of Classes belongs. In addition to the classes, there is also an important one in Java Method, the each Javaprogram must contain that main method. What a method is and what function it is in one Java program we will show you in a separate article. You can find them main method in the program code in the 3rd line. It gives the starting point of the Java program at. The main method always looks like this for introductory programs in Java, so you should remember the code carefully.

The curly braces of the main method give the exact Program start and the End of program at. In between there are instructions on how the program should behave. In this case it consists of a single statement:

This instruction is in the 5th line of the program and displays the text within the quotation marks on the screen. So in our example. The other lines have little to do with the output, but are important nonetheless.

Save and compile Java program

As you've probably already noticed, it's called class of Java program. That's why you have to file under the Names Save "HelloWorld.java" so that the names match and the Compiler does not report an error. The Compiler is for that Java source code, that you wrote into one Bytecode to convert so that the Java processor can execute it and thus start the program. So he translates your program into a episode of Zeros and Ones, so that the computer can process it. The "HelloWorld.class" file is then generated. Besides, he does Compiler also alert you to mistakes. For example, if you forget a bracket, it gives Compiler an error back. The Compiler so check that you have spelled everything correctly. To be able to compile, you first have to switch to the directory on the console in which you have saved your file. You can then compile your program with

You can find out more about this in our video My first program.

Bugs

The Compiler however only recognizes error in the Syntax. However, do you have one logical error in your program, then it recognizes Compiler not this one. So assuming you write your program without errors and there are no errors in the syntax, it can still happen that the program does not serve its purpose and is therefore incorrect. In this context one speaks of a so-called Bug. For example, when the output of the program is instead. The Java program can be compiled without any problems, but the result deviates from the actual goal. That is why it is important, especially with larger programs, to plan what it is supposed to do about such programs Bugs to avoid.

Start the Java program

After saving, you can now run your program from the console start. To do this, you simply give the order

on the console. You can also find the exact procedure for this in the video My first program.

System.out.println

If you want, you can also let several lines of text appear on the screen. To do this, you simply have to use the instruction several times. The whole thing could look like this, for example:

The program then outputs the following on the screen:

The function converts each instruction into a extra line issued. These are so-called Strings.

Strings? Yes you heard right. The sentences in the quotation marks are Strings! This allows us to string characters together, which are then called String be recognized. So we can then display them on the screen.

Layout and comments

In the Java basics you should also learn how to use your Clear source code and make it easy to understand. To do this, you should get into the habit of using a clean and consistent layout. Important parts of the Source codes you also need to comment so that other programmers can easily understand your code.

There are single line and multiline comments. The whole thing looks like this:

Most programming languages ​​and text editors will Comments marked green. A single line comment begins with "//". Everything that stands after "//" is ignored by the compiler and is only used for understanding for you as a programmer or for others who want to understand your program. You can also multiline comments insert. To do this, start your comment with "/ *" and end it with "* /". The compiler evaluates everything between these characters as a comment.

Now you're already at the end of the Java basics arrived. We still want you two important pointers give. For one thing, always make sure that you have the curly braces don't forget once you get one class or method write. On the other hand, you have to put a behind every instruction, such as always semicolon put in the end!