Learn Java programming - the basics
Related Videos: Java Tutorial for Beginners - Made Easy (May 2024).
Java is one of the most famous programming languages in the world and is used on over three billion devices. If you want to learn Java, you will get a first introduction here.
Java: How to create your first program
To be able to code correctly with Java, you need a development environment, in short: IDE. IntelliJ or Visual Studio is very suitable for Java. Various first programs can be found in the picture gallery at the end of the article.
- In Java, every command to be executed must be in a class. Important: The main program is usually in a class called "Main". However, you can also add more classes. So our first program starts with the code "class Main {".
- Every Java application also has a starting point, which is a method (or "function") called "main". Next, you need to add the code "public static void main (String [] args) {". In Java, "public" means that anyone can access this function. The word "static" means that the method can be executed without an instance. That means: When creating the object, no memory is reserved for attributes / methods. These elements are created during compilation. Compiling means that the program code you have written is converted into an executable code ("machine language"). Furthermore, "void" means that this method does not return anything. The name of this method is "main".
- Next, we want the program to output the word "hello". To do this, write "System.out.println (" Hello ");". Your first Java program is now ready. Don't forget to add two "}" at the end.
Comments in Java
To make it easier for you to program, you can add various comments.
- You can create a one-line comment as follows: »// This is a comment«
- You can also add a comment over several lines in the same way. This begins with "/ *" and ends with "* /".
- Finally, you can also add a documentation comment. To do this, write "/ **" at the beginning and "* /" at the end. Each new line in this comment begins with a "*". A documentation comment is useful if, for example, you want to state the version of the program and the author. This special type of comment is used to document the applications directly in the source code. Afterwards you can generate a detailed documentation of the program. For smaller programs, however, this is not so important for the time being.
Java: create variables - how it works
To work in Java, you need to create variables.
- To do this, you have to deal with the normal data types in computer science. First there is the integer. This is an integer. The command »int var1 = 42;« defines, for example, the variable "var1" and assigns it the integer value "42".
- You will also often find strings in Java. These can be words or entire sentences. The command »String name =" Hans ";« assigns the value "Hans" to the variable "name".
- You can also define comma numbers in Java. This can be done, for example, with the "double var2 = 15.9;" command.
- Next up is the "Char". It is a single character. A command to do this could be "char var3 = 'Z';" in Java.
- Finally, there is the "Boolean" data type, which can have two states: True or False. In Java, for example, you can do this with the command "boolean var4 = true;".
Mathematical operators in Java
Just like in other programming languages, there are different operators in Java.
- As in classical mathematics, you can add two summands with »+«. With "-" you subtract one subtrahend from one minute. With "*" you can multiply two factors and with "/" you can divide a dividend by a divisor to get the quotient.
- There is also a function called "Modulo" in Java. The "%" character is used for this. An example would be the code "int res = 23% 6;". 23 divided by 6 gives 3, with the number 5 remaining. This number is stored in the "res" variable.
- You can also calculate with variables (see previous paragraph). However, the numbers that are stored in the respective variables are used in the calculation.
Java: Increment & Decrement
These functions are essential, especially for loops.
- For example, you can store an integer in the "x" variable. With the code »x = x + 1;« you can increase the value by 1. However, to save space, you can also write "++ x".
- Likewise, the value can be reduced by 1 with »--x«.
- However, you can differentiate between "Prefix" and "Postfix" (or in German: "Prefix" & "Suffix"). An example is the code "int x = 34;" ⮩ "int y = ++ x;". In this case, the value 34 is first increased by 1 and only then assigned to the variable y. After this step, both variables ("x" & "y") have the value "35". The whole thing is called "prefix".
- Instead of "++ x" you can write "x ++". First the value of the variable "x" is assigned to the variable "y" and then the value of "x" is increased by 1. While "y" has the value 34, "x" already has the value 35.
- The "assignment operators" are also very practical. Instead of "num2 = num2 + num1" you can also write "num2 + = num1". The whole thing also works with subtractions (»- =«), multiplications (»* =«), divisions (»/ =«) and divisions with remainder (»% =«).
Put together strings
As you already know, a string is a string in computer science.
- In Java - like in other programming languages - you can put together a string from several other strings.
- An example of this is the code »String firstN =" Hans ";« ⮩ »String lastN =" Peter ";« ⮩ »String name = firstN +" "+ lastN;«.
Java: User input received
In order for the user to be able to interact with the Java program, it is practical to read the keystrokes.
- To do this, first write the command »import java.util.Scanner;« to import the required module.
- Then create a new scanner with »Scanner Input = new Scanner (System.in);«.
- With the command »System.out.println (Input.nextLine ());« you could now have the entire entered line output.
- Alternatively, you can also use nextByte (), nextShort (), nextInt (), nextLong (), nextFloat (), nextDouble (), nextBoolean () or next () «Use. This in turn depends on the type of data you want to receive.
Learn Java Programming - Part 2
Now that you know the basics, you can take a look at our part 2 of the programming guide. There you will learn more about conditions and loops in Java.
In the next practical tip, we will show you how you can easily convert floating point numbers.