The first program that we write in Java is of course “Hello World”. Open your Notepad and write the following code. I will emphasize the word “write” here, by copying the examples you will not learn anything. I thought about adding pictures of code at this point to make it more difficult :), but later copying will be need.

Copy your to to new notepad file and save it anywhere with extension .java instead of .txt. The title of the file must be the same as your class. In our case that would be “Hello” i.e. Hello.java, case sensitive!

Files with extension .java can later be compiled to code.

Let’s discuss this example line by line.

1. public class Hello – this is nothing but a public class named Hello. A class is, in short, a cell of information, it contains various data – variables, methods (functions) and other objects. If you do not understand it yet, do not worry.

2. public static void main (String [] args) – this is the main method, it starts the program operation from it and in it we call other methods, or create objects responsible for it. The arguments in the form of String table are given in brackets. Later we will see how you can use it.

3. System.out.println (“Hello World”); – we are using the System library that adds a new line “\n” at the end of the printed text.

Please note that the text (Hello World) is given in the quotation marks and the instruction ends with a semicolon, and also that the appropriate sections of the code – the so-called code blocks – we show/put using braces {}. Their omission will make the program not work.

Launch the program:

First run the console (cmd), and go to the appropriate folder. Move using the commands:

  • d: (or c:; e: etc.) goes/moves you to the appropriate disk
  • cd folder_name – goes/moves yuo to the appropriate directory
  • cd .. – moves you to the higher level directory

When we are in the right directory, in my case c: / java /, we have to compile using the javac command we already know. We do this by typing:

javacĀ nameOfClass.java

We can not forget about the extension .java!

As a result of this operation, a new file with the .class extension with the same name as our created file should appear in the same folder. It contains a bytecode that can be executed by a Java virtual machine.

We will finally start our program by entering:

java class_name

in this case, we do not add the .class ending. As a result of our actions, we should get something like this:

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here