How to Write a Program in Java

In this tutorial I'll explain how to write your first program in Java.

What do you need to program in Java? To write a program in Java you can use any text editor. Also the Windows notepad or nano on Linux is fine. So, you must have already installed the compiler and the Java virtual machine on your PC. Alternatively you can use an online Java editor to practice.

Write your first program in java

Choose the name you want to give the program without leaving spaces. For example, in this case I have chosen as helloworld name.

Then open the text editor (eg Windows Notepad) to start writing the program.

open the Windows text editor

Type the class statement with the name of the program.

Then open and close two braces.

class helloworld {

The class statement tells the computer that the program is called "helloworld".

Now write the main class public statid void main (String [] args) of the program.

class helloworld {
public static void main(String[] args) {

In the main class, you can type the instructions that the computer must perform.

In the java language you must always add the semicolon; at the end of each instruction.

In this case, write System.out.println("Ciao");

It is the instruction to print a message on the screen.

class helloworld {
public static void main(String[] args) {

When you're done, save the program file with the extension .java.

the source file in Java on Windows Notepad

If you are using Windows Notepad, click on File in the upper left.

Then select the Save As menu item.

select file and save as

I suggest you give the file the same name as the program ( eg. helloworld.java ).

You could get confused if you give it a different name.

save the program source code

Note. Remember to always add the .java extension to the program name when you save it. Do not just write helloworld as the file name. You must type the full name helloworld.java. Otherwise the compilation does not work.

Now there is a file in your PC folder helloworld.java.

It is the source code of the program.

the java source file has been saved

Once you have saved the source file, you can compile it and run it.

How to compile Java

Open the command prompt and go to the folder where you saved the source file ( helloworld.java ).

Then type the javac command to compile the file

javac helloworld.java

If there are no compilation errors, the prompt will flash again on the next line.

The compiler created a bytecode (executable) file with extension .class in the same directory.

In this case it is the file helloworld.class.

the compilation file has the extension .class

At this point you can run the program.

Run the java program

Run the bytecode using the command java.

On the command prompt, type:

java nomeprogramma

It is not necessary to type the .class extension.

The Java language interpreter reads and executes the bytcode file ( helloworld.class ).

The message "ciao" is printed on the screen

the output of the program

You have created, compiled and executed your first java program

In this lesson you learned how to write, compile and run a java program.

Is it too simple? It is only the first lesson.

Report a mistake or post a question