Skip to main content

First Java program

Assuming that you already can run JAVA in your machine.
Let's learn the java basic to begin your first java journey

First of all Java will run the code in main function and main function will be in the class of your choices
1. Create a new folder where you want to store your codes.

2. Open vs code and open that folder you created before

Screenshot 2024-07-25 at 14.06.13.png

Lets create a new file name MyFirstJava.java 

Screenshot 2024-07-25 at 14.07.29.png -> Screenshot 2024-07-25 at 14.09.30.png

 

Now, you will get an empty java file.
Let's create a simple java program.

A java program will run in a classes.

If a class have main function. it will be able to run in commands.

So, first lets create a class and the main function that our program will run in.

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

    }
}

Right now you don't have to care what is this mess and what it will do. Lets skip the headache for now.

As you can see we have a class MyFirstJava and a function main in it

Now try the most basic thing in programming : Printing hello, World!

in Java the way you can print something out in the terminal is using System.out.print or System.out.println

class MyFirstJava {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Try running it and observe the result

After that let's learn about the difference between print and println

class MyFirstJava {
    public static void main(String[] args) {
        System.out.println("This is first println");
        System.out.println("And this is second println");
        System.out.print("This is the print function");
        System.out.print("And this is the second print function");
    }
}

Try running and observe the difference between print and println

Now you already have your first java program