HomeSOFTWARESJava: How To Create Your First Program

Java: How To Create Your First Program

To code correctly with Java, you need a development environment, in short: IDE. IntelliJ or Visual Studio are 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 the “Main” category. However, you can also add other courses. So our first program begins 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) {” to it. In Java, “public” means that anyone can access this function. The word “static” means that the method can be executed without an instance existing. This means that no memory is reserved for attributes/methods when creating the object. These elements are already created during compilation. 

Compiling means that the program code you have written is converted into executable code (“machine language”). Furthermore, “void” means that this method does not return a value. The name of this method is “main.” Next, we want the program to print out “Hello.” To do this, write “System.out.println(“Hello”);”. Your first Java program is now complete. However, don’t forget to add two more “}” at the end.

Comments In Java

To make it easier for you to program, you can add various comments:

  1. To create a one-line comment: “//This is a comment.”
  2. You can also add a comment over several lines in the same way. This starts with “/*” and ends with “*/.”
  3. 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 helpful if you want to specify the version of the program and the author, for example. This particular type of comment documents the applications directly in the source code. Afterward, you can have detailed documentation of the program generated. However, for smaller programs, this is not so important for the time being.

Java: Create Variables – How It Works

To work in Java, you need to create variables.

  1. You have to deal with the normal data types in computer science for this. First, there is the integer. This is an integer. For example, the command “int var1 = 42;” defines the variable “var1” and assigns it the integer value “42”.
  2. So, you will often find strings in Java. These can be words or whole sentences. The “String name = “Hans”;” command assigns the value “Hans” to the variable “name.”
  3. You can also define decimal numbers in Java. This can be done, for example, with the command »double var2 = 15.9;«.
  4. Next up is the “Char.” This is a single character. A command for this could be “char var3 = ‘Z’;” in Java.
  5. Finally, there is the Boolean data type, which can have two states: true or false. In Java, for example, this can be done with the command “boolean var4 = true;”.

Mathematical Operators In Java

Just like in other programming languages, there are different operators in Java:

  1. In classical mathematics, you can add two summands with “+.” Use “-” to subtract a subtrahend from a minute ending. You can multiply two factors with “*” and divide a dividend by a divisor with “/” to get the quotient.
  2. Furthermore, there is a function in Java called “Modulo.” The “%” character is used for this. An example would be “int res = 23 % 6;”. 23 divided by 6 equals 3, with the remainder being 5. This number is stored in the “res” variable.
  3. You can also calculate with variables. However, calculations are made using the numbers stored for the respective variables.

Java: Increment & Decrement

These functions are essential for loops in particular: For example, you can store an integer in the variable “x.” You can increase the value by 1 with the code “x = x + 1;”. However, to save space, you can also write “++x.”

  1. In the same way, the value can be reduced by 1 with “–x.”
  2. However, you can differentiate between “Prefix” and “Postfix”. 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 is it assigned to the variable y. After this step, both variables (“x” & “y”) have the value “35”. The whole thing is called a “prefix.”
  3. You can also write “x++” instead of “++x”. First, the value of variable “x” is assigned to variable “y,” and then the value of “x” is increased by 1. While “y” has the value 34, “x” already has 35.
  4. The “Assignment Operators” are also handy. Instead of “num2 = num2 + num1” you can also write “num2 += num1”. This also works for subtraction (“-=”), multiplication (“*=”), division (“/=”), and division with remainder (“%=”).

Assemble Strings

As you already know, a string is a character string in computer science.

  1. In Java – as in other programming languages ​​- you can compose a string from several other lines.
  2. An example of this is the code “String firstN = “John”;” ⮩ “String lastN = “Peter”;” ⮩ “String name = firstN + ” ” + lastN;”.

Java: Received User Input

For the user to interact with the Java program, it is practical to read out the keystrokes:

  1. To do this, first, write the command “import java.util.Scanner;” to import the required module.
  2. Then create a new scanner with “Scanner input = new Scanner(System.in);”.
  3. With the command “System.out.println(Input.nextLine());” you could now print out the entire input line.
  4. Alternatively, you can also use “nextByte()”, “nextShort()”, “nextInt()”, “nextLong()”, “nextFloat()”, “nextDouble()”, “nextBoolean()”, or ” next() ” use. Again, that depends on the type of data you want to get.

Also Read: Internet Of Things, Which Programming Languages ​​To Learn

Techno Publishhttps://www.technopublish.com
Technopublish.com is a reliable online destination for tech news readers who want to keep themselves updated on current innovations and advancements on topics related to technology.
RELATED ARTICLES

RECENT ARTICLES