Sitemap
Ayushi Trivedi

Ayushi Trivedi

First Java Program

4 min readMar 14, 2022

You must first construct a class that contains the main function to create a simple Java program. Let’s start with the prerequisites.

The Java Hello World Example is required.

The following software or application must be installed before any Java program may be run.

  • Install the Java Development Kit. Download and install the JDK if you don’t already have it.
  • Set the JDK/bin directory’s path.
  • Make a Java program.
  • Run the Java program after it has been compiled.

Creating Hello World Example

Let’s create the hello java program:

public class Simple {

public static void main(String args[]) {

System.out.println(“HelloWorld! we are SARTECH”);

} }

Test It Now

To compile: javac Simple.java

To run: Java Simple

The flow of Compilation:

The Java compiler translates the source code into bytecode when we build a Java program with the javac tool.

The First Java Program’s Parameters

Get Ayushi Trivedi’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s look at class, public, static, void, main, String[], and System.out.println().

  • In Java, the class keyword is used to declare a class.
  • The visibility is represented by the public keyword, which is an access modifier. It indicates that it is visible to everyone.
  • The keyword is static. Any method that is declared as static is referred to as a static method. The main advantage of the static approach is that it does not require the creation of an object to use. Because the main() method is executed by the JVM, there is no need to create an object to call it. As a result, it conserves memory.
  • The method’s return type is void. It indicates that it does not return any information.
  • The program’s starting point is represented by the main.
  • The command-line argument is String[] args or String args[]. We’ll go over it in the next segment.
  • To print a statement, use System.out.println(). The system is a class, out is a PrintStream class object, and println() is a PrintStream class method. In the following part, we’ll look at how the System.out.println() statement works inside.

Details about the Hello Java program’s internal workings

We constructed a Java Hello World program in the previous part and learned how to compile and run a Java program. In this section, we’ll look at what happens during the compilation and execution of a Java programme. In addition, several questions based on the first program will be seen.

What happens throughout the compilation process?

The Java file is compiled by Java Compiler (which does not interface with the operating system) at compile time, which translates the Java code into bytecode.

What occurs when the programme is running?

The following stages are carried out at runtime:

The Classloader: Subsystem of the JVM is responsible for loading class files.

Bytecode Verifier: Checks code fragments for illegal code that could compromise object access privileges.

Interpreter: The instructions are then executed after reading the bytecode stream.

Is it possible to save a Java source file with a different name than the class name?

Press enter or click to view image in full size

If the class isn’t open to the public, yes. It’s illustrated in the diagram below:

To compile: javac Simple.java

To run: Java Simple

It’s worth noting that we compiled the code with the file name but ran the programme with the class name. As a result, we can save a Java programme that isn’t named after a class.

Is it possible to have many classes in a single Java source file?

Yes, as illustrated in the diagram below:

--

--