Home Blog Archive Software Java programming - learn the Basics

Java programming - learn the Basics

  • Sep 23, 2020
  • 102
  • 0

Java is one of the most popular programming languages in the world and is used on over three billion devices. If you want to learn Java, here is a first introduction.

Java: how to create your first program

To be able to work with Java properly code, you will need a development environment, abbreviated IDE. Very good for Java is IntelliJ or Visual Studio. Various first programs can be found in the gallery at the end of the article.
  • In Java each instruction to be executed must reside in a class. Important: The main program is located usually in a class called "Main". However, you can also further add classes. Our first program starts with the Code "class Main {".
  • Every Java application has a starting point, it 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 everyone can access to this function. The word "static" means that the method can be executed without an instance exists. That is to say: When you Create the object, no memory is reserved for attributes/methods. These elements are already generated at Compile time. Compile means that the written program code is converted into executable Code ("machine language"). Furthermore, the void means that this method returns no return value. The Name of this method is "main".
  • Next, we want to now that the program outputs the word "Hello". To do this, you write "System.out.println("Hello");". So your first Java program already finished. Do not forget, however, at the end of two more "}" add.
Java: program to create

Comments in Java

So you have it when programming easier, you can add different comments.
  • A single-line comment, you create these steps: "//This is a comment"
  • Similarly, you can also add a comment over multiple lines. This begins with "/*" and ends with "*/".
  • Finally, you can also add a documentation comment. For that, you write at the beginning "/**" and "*/". Each new line in this comment begins with a "*". A documentation comment is useful, for example, to specify the Version of the program and the author. This special type of comments is used to document the applications directly in the source text. Afterwards you can have a detailed documentation of the program to generate. In the case of smaller programs, however, this is not for the time being still is so important.
Comments in Java

Java: creating variables - how to

To work in Java, you need to create variables.
  • For this you need to deal with the normal data types in computer science. Here, first there is the Integer. This is a whole number. The command "int var1 = 42;" is defined, for example, the Variable "var1" and assigns it to the Integer value "42".
  • In addition, you will find in Java are often Strings. These can be words or whole sentences. The command "String name = "Hans";" assigns the Variable "name" with the value "Hans".
  • Furthermore, you can also use floating-point numbers in Java define. This is, for example, with the command "double var2 = 15.9;".
  • Next, there is the "Char". It is a single character. A command could be in Java "char var3 = 'Z';".
  • Finally, there is the Boolean data type can have two States: True or False. In Java this is, for example, with the command "boolean var4 = true;".
Java: variables to create a

Mathematical operators in Java

In the same way as in other programming languages, there are operators in Java various Opera.
  • As in the classical mathematics, you can use the "+" two summands add up. With "-" subtract a Subtrahe of a Minuenden. With "*" can you multiply two factors with each other and with "/" means a dividend by a Divisor, to get the quotient.
  • Furthermore, in Java there is a function called "Modulo". For this purpose, use the "%" character. An example of the Code "int res = 23% 6 would be;". 23 divided by 6 equals 3, the number 5 remains as a residual left over. This number is stored in the variable "res".
  • In addition, you can also use variables to count (see previous paragraph). There is, however, expected with the Numbers that are stored in the respective variables.
Mathematical operators in Java

Java: Increment & Decrement

Especially in the case of loops, these functions are indispensable.
  • For example, you can specify an Integer for the variable "x" behind. With the Code "x = x + 1;" you can increase the value by 1. In order to save space, you can also write "++x".
  • Just as the value with the "--x" to 1 reduce.
  • Here, you can, however, differentiate between "Prefix" and "Postfix" (or in German: "prefix" & "Suffix"). An example of the Code "int x = 34;" ⮩ "int y = ++x;". In this case, first, the value of 34 is increased by 1 and then the variable y is assigned. After this step, both variables ("x" & "y") the value of "35". The Whole is called a "Prefix".
  • Instead of "++x" you can also write "x++". It is initially assigned the value of the variable "x" to the variable "y" and then the value of "x" increases by 1. While "y" has the value 34, has "x" already has the value 35.
  • Also very useful is the "Assignment Operator". Instead of "num2 = num2 + num1", you can also write "num2 += num1". It also works for subtraction ("-="), multiplication ("*="), division ( "/ = " ), and in the case of divisions with remainder ("%=").
Java: Increment & Decrement

Strings put together

As you already know, a String in computer science a string.
  • In Java, you can set - as well as in other programming languages - a String from several other Strings together.
  • An example of this is the Code "String firstN = "Hans";" ⮩ "String lastN = "Peter";" ⮩ "String name = firstN + "" + lastN;".
Java: string together

Java: user input get

Thus, the user with the Java program can interact with, virtually, the keyboard inputs to be read.
  • To do this, first write the command "import java.util.Scanner;" to import the required module.
  • Then you create a "Scanner input = new Scanner(System.in);" a new Scanner.
  • With the command "System.out.println(input.nextLine());" could you leave now, the entire line entered spend.
  • Alternatively, you can use "next-byte ()", "next short ()", "nextInt ()", "next long ()", "nextfloat ()", "next double ()", "next Boolean()" or "next ()". That in turn depends on the type of data that you would like to receive.
Java: user input get

Java learning to program - part 2

Once you now the Basics, you can be our part 2 of the programming Guide to watch. There you will learn more about conditionals and loops in Java.
In the next practical tip we show you how you can floating point numbers, simply convert can.

Latest Videos

This first Java program writes "Hello". It is important that the main program is always in a class called "Main". However, you can also create additional classes.

This first Java program writes "Hello". It is important that the main program is always in a class called "Main". However, you can also create additional classes.

To facilitate programming, you can add comments.

To facilitate programming, you can add comments.

As in other programming languages, you can also define Java variables.

As in other programming languages, you can also define Java variables.

With Java, you can expect quite easily.

With Java, you can expect quite easily.

You can increase in various ways the value of a variable.

You can increase in various ways the value of a variable.

Different Strings can also be put together easily.

Different Strings can also be put together easily.

So easily you can get the keyboard input from the user.

So easily you can get the keyboard input from the user.

YOU MAY ALSO LIKE

0 COMMENTS

LEAVE A COMMENT