Java tutorial: the basics for beginners
Java is an object-oriented programming language, which we will introduce to you in this tutorial. Web applications, desktop applications and apps are primarily programmed with Java.
Java tutorial: preparations
Before you can actually start programming with Java, you need to do some preparation.
- Install the latest Java SDK on your computer. Simply download it from our download area and follow the instructions during installation.
- In the next step you need a development environment. There are various options here, all of which have advantages and disadvantages. The best known are NetBeans, Eclipse and IntelliJ. Eclipse is suitable for beginners, which is why the following steps are related to Eclipse.
- Start Eclipse and then click on "File", "New" and then "Java Project". Enter a name for the project here and press "Finish".
- On the left side you will find the Package Explorer, here all folders and files of the project are listed. So far, the project only contains the "src" folder. Right click on the folder and select "New" then "Class". In the window that opens, select a class name and check the box next to "public static void main (String [] args)". Then press "Finish".
- Tip: We will also show you the most important steps in our picture gallery at the end of the article.
Java: Hello World tutorial
After the preparations you can now start with the actual programming of the first program.
- You now have the basic structure of a class including a main method. This main method is always called first when the program is started and the code it contains is executed.
- The main method already contains the comment "TODO Auto-generated method stub". A comment always begins with "//" and is not taken into account during execution.
- Add the line "System.out.println (" Hello World! ");" To your program directly below the comment. When executing the program, this command should display the message "Hello World!" output.
- Click the green play icon in the bar at the top. The program is started in this way. In the lower half of the Eclipse window you will find the "Console". Here the desired "Hello World!" output.
Java tutorial: Variables and data types
In order to make our programs more complex, we need variables that each have their own data type.
- The most important construct of any programming language are the variables. A value can be stored in a variable, which in turn can be changed by operations.
- There are a total of eight primitive data types in Java. The following types are the most important and are used most frequently.
- int: An int variable can store an integer. This number must be between -2, 147, 483, 648 and 2, 147, 483, 647.
- double: A floating point number, however, can be stored in a double variable. For example, an amount of money can be saved.
- char: Any single character can be stored in a char variable.
- boolean: A Boolean variable contains a truth value. It can only take one of two possible values: true or false.
- A variable is declared and initialized as follows: "[data type] [name] = [value];". An example would be: "int x = 4;"
- You can perform simple arithmetic operations on numbers: "int a = 4; int b = 5; int sum = a + b".
- Of course you can also save text. To do this, you need the keyword "String". While this is not a primitive data type, it behaves similarly. So we can also save the "Hello World" expression in a variable: String hello = "Hello World"; System.out.println (hello);
Java tutorial: Additional control structures
Now that we've worked with variables, we're now learning conditional and repeated calls.
- For conditions, we first have to get to know the operators for comparisons: == checks for the equality of two values or variables. ! = is the exact opposite: inequality. <, and> = corresponds to the symbols for smaller, smaller equal, larger and larger equal. These operators almost always encounter us in conditions.
- In addition to a loop, we can also conditionally execute a block. This works with an if query: if ([condition]) {// statements}. For example, we can use this to ensure that there is no division by 0.
- if (persons> 0) {bananeProPerson = bananas / persons}. In this example, the "bananas per person" are only calculated if we have more than 0 people.
- Programs should now become even more complex: We would now like to output all numbers from 1 to 10 on the console. We could do this ten times System.out.println (...); retype. However, this quickly becomes too complicated if, for example, you also want to output all numbers up to 100. There are so-called loops for this. This repeat defined code.
- A for loop is structured like this: "for ([start value]; [condition]; [update]) {// instructions}". The start value contains a variable that contains the start value. This is usually an int variable, which we can only create here (eg: int i = 1). The condition determines how long the loop is executed (eg: i <= 10). The update determines what is done after each call to the loop (eg: i = i + 1).
- We then output the values 1 to 100 as follows: for (int i = 1; i <= 100; i = i + 1) {System.out.println (i);}
Next steps in the Java tutorial
Learning Java is not a goal for a few hours. It will take months before you fully understand the programming language.
- This tutorial is primarily followed by practice. Get to know the data types and solve some problems in your own applications. Mathematical problems are particularly well suited: For example, output the square numbers of all numbers up to 10. Or output all numbers up to 1000 that are divisible by seven.
- But there are also many more language constructs that we have not yet got to know here: These include, for example, the switch statement or the while loop.
- The platform YouTube offers an opportunity to expand knowledge. There are countless Java tutorials here. There are also many professional video tutorials. You can find them on the Video2Brain, Udemy or Coursera platforms, for example. However, many of these offers are in English.
- Alternatively, there are some good books on Amazon that convey a very extensive knowledge of Java.
You will also spend many hours debugging on debugging while programming. One of the most common errors is the NullPointerException. You can read how to fix this in the next practical tip.