Hello World! First Java Program

Photo by Andrew Neel on Unsplash

Hello World! First Java Program

Writing my first code in Java.

Shubham Deshmukh's photo
Shubham Deshmukh
·Jul 3, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • The requirement for running Java.
  • Writing Hello Java Example
  • Compiling and running the code

Hello Friends, Today we will be understanding and writing our first java code from scratch. We will go through the pre-requisites and later will go through the code.

The requirement for running Java.

For executing any Java program, the following steps must be followed.

Install the JDK if you don't have installed it, download the JDK and install it as per your operating system. Set path of the JDK/bin directory. You can also go through this detailed setup for java.

We won't be using any IDE like Eclipse or IntelliJ to keep it simple.


Writing Hello Java Example

Save the below code in the HelloWorld.java file.

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

Compiling and running the code

To Compile the code execute.

javac HelloWorld.java

Once the file is complied execute the below command to run it.

java HelloWorld

Screenshot 2022-07-03 at 1.13.16 PM.png

Understanding the program

  • class keyword is used to declare a class in Java. We have declared HelloWorld class with it.
  • public keyword is an access modifier that represents visibility. It means it is visible to all. There are other modifiers like private and default as well.
  • static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. The static keyword is used for a constant variable or a method that is the same for every instance of a class.
  • void is the return type of the method. It helps us to understand what is the expected output from the method after its execution flow. void means it will not return anything.
  • main represents the starting point of the java program.
  • String args[] is used for command-line arguments. It's used to pass values to your application when run via the command line.
  • System.out.println() is used to print statement in java.

Thanks for reading. I hope this story was helpful. If you are interested, check out my other articles.

you can also visit shubhamdeshmukh.com

Did you find this article valuable?

Support Shubham Deshmukh by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this