Java – First Java program

  • Post category:Java
  • Reading time:6 mins read

Java First Java program

To create a simple Java program, you must create a class because Java is an object-oriented language, so you must write some code inside a class. The class contains the main method. On this page, we will learn how to write the simple Java program after installing JDK and configuring the path.

Basic steps

  • Download Java and install it, if you don’t have installed it.
  • Set Java path otherwise Set Java path otherwise when we will compile java program , we get exception javac is not recognized as an internal or external command
  • Create the java program
  • Compile and run the java program

Java Program Example

class Demo{  
    public static void main(String args[]){  
     System.out.println("Hello Proedu");  
    }  
}  

Save the above file as Demo.java

To compile this Program:javac Demo.java
To run this Program::java Demo
// Output
Hello Proedu

At Compile Time :

In above program ,when we compile Java program using javac Basic.java, compiler converts the source code into byte code.

internal_java

Let’s understand what’s in the Java program and its key points.

  • class keyword is used to declare a class in java.
  • public keyword is an access modifier which represents visibility. It means it is visible to all.
  • static is a keyword. If we declare any method as static, it is called as the static method.Static method is property of class so the benefit of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn’t require to create an object to invoke the main method and it saves memory.
  • void is the return type of the method. It means it doesn’t return any value.
  • main method is the starting point of the program and the logic must be inside the main() method to start a program. If a java class is not having a main() method, it causes compilation error.
  • String[] args is used for command line argument.
  • System.out.println() is used to print statement. Here, System is a class, out is the object of PrintStream class, println() is the method of PrintStream class.

There are many valid ways to write main method in java .

Valid java main method signature

1. public static void main(String[] args)
2. public static void main(String []args)
3. public static void main(String args[])
4. public static void main(String… args)
5. static public void main(String[] args)
6. public static final void main(String[] args)
7. final public static void main(String[] args)
8. final strictfp public static void main(String[] args)

Invalid java main method signature

1. public void main(String[] args)
2. static void main(String[] args)
3. public void static main(String[] args)
4. abstract public static void main(String[] args)

First java program Example

Write a program in Notepad

Compile above program using javac Basic.java command

Run above program using java Basic command

Happy Learning 🙂