Writing Your First Program in Java

Writing Your First Program in Java

After learning the history of Java, now it is time to write your first program in Java but before you write, firstly you have to install the JDK program which facilitates a development environment to write Java Program, and also learn a few important parameters you will use during writing your first program in java.

In this blog, we will understand how to install the required software in order to run your first Java program in your system.

How to install Java compiler in your Windows/Mac

We need to install two applications/software, the first one is Java Development Kit and the second is a compiler. There are a variety of compilers but here we will be using IntelliJ. Currently, IntelliJ is being used the most there are others too such as Java Visual Studio Code (VS Cod, Eclipse, etc. You can also go for an Online Compiler which will be easy for you to use but every programmer prefers to use the compiler applications.

Step 1

Install Java Development Kit (JDK) from its official website. Click here for the official website.

Step 2

The latest version of JDK is Java 18 and the older version is Java 18. So, scroll down till you find “Java SE Development Kit 18.0.2.1 downloads “. Here, you will find three options: Linux, Windows, and macOS. Choose your operating system. (Mine is windows so I will be choosing windows).

install jdk

Step 3

Now, click on the x64 installer option ( Here’s the direct link for windows ). Save it at your preferred location and then right-click the file and click “Run as administrator”.

how to install java

Step 4

Press ‘next’ and then after installing, go to C: drive > Program Files > Java > jdk-18.0.2.1 > bin.

install jdk

Step 5

Now, copy the path ( C:\Program Files\Java\jdk-18.0.2.1\bin ) and go to the search bar. Here, type environment variables.

best way to install java in your pc

Step 6

A pop-up will appear, click on “Environment variables”. After opening it scroll down and you will find “Path” in the ‘System Variable’,

install jdk and java

double click on it and press new and past the path, which you have copied earlier from the file location.

how to run java in computer

Step 7

After pressing Okay, go back to ‘System Variable’ and press “New”; name it as “JAVA_HOME” and paste the same path as copied earlier ( C:\Program Files\Java\jdk-18.0.2.1\bin) and press Okay, your work is done.

installing java in laptop

Go to search and open Command Prompt to check about the installation success. Type “Java –version”, and you will get to see all the details about the version and runtime as shown below:

How to install java in windows

Downloading Compiler

Now, the compiler where you will be running your program is IntelliJ by Jetbrains. I will be giving a direct link to the website here,

Select your operating system and go for the community version as it is free and is built on open source.

Here’s a video of how to download IntelliJ

If your working area doesn’t open directly follow these steps:

how to open project in java

 

  • Step 1: Right-click on “src” then click “New” and Press on “Java Class”.

intellij

  • Step 2: Type your first project name for eg. myClass, newClass, first program.

hello world in java

Also read, Difference between var vs let vs const in JavaScript | TDZ

How is Java Code compiled?

The programs in java are not compiled into executable files instead it is compiled into bytecode; which is then executed at runtime by the JVM (Java Virtual Machine). When we utilize the javac compiler, Java source code is converted into bytecode. The file extension .class is used to save the bytecode on the hard drive. The just-in-time (JIT) compiler converts the bytecode before the program is executed. The outcome is machine code, which is subsequently executed after being fed into the memory.

The Java code compiles twice in order to be executed, firstly the java program has to be compiled into bytecode and when bytecode runs it, it converts into machine code. When initially required, the JVM loads the Java classes and bytecode into memory after being compiled to machine code. In contrast to other languages like C/C++, where programs must first be translated into machine code and linked to produce an executable file, this language does not require this.

Also read, Simple Explanation of Stack and Queue with JavaScript

Parameters to know before writing Java Programme

The basic ones that you need to know before writing your Java Programme are class, public, static, void, main, String[], System.out.println().

  • class: A keyword in Java that is being used to declare a class.
  • public: A keyword that is a visibility-related access modifier. It means that everyone can see it.
  • static: Any method that has been declared static is referred to as a static method. The main benefit of using a static method is that you don’t need to build an object in order to use it. It is not necessary to create an object in order to call the main() method because the JVM runs it. This preserves memory.
  • void: It is a return type method which means it doesn’t return
    any type of value.
  • main: It is the entry/starting point of a program.
  • String[] args or String args[]: It is used as a command line argument. We will learn about this in the next class.
  • System.out.println(): It is used for printing statements. In this case, println() is a method, and out is an object of the PrintStream class. System is also a class. In the following part, we’ll go over how the System.out.println() command internally functions.

Also read, Simple Explanation of Sorting Algorithms with JavaScript | Part 2

Writing First Program in Java!

  • Step 1: Open a text editor, and then enter the code as described above.
  • Step 2: Save the file as Hello.java in step two.
  • Step 3: Open a command prompt and navigate to the directory where your first Java program was saved. Assume this was in the C drive.
  • Step 4: Enter javac To build your code, type Hello.java and press Return(Enter KEY). By using this command, the Java Compiler will be called and asked to compile the supplied file. The command prompt will advance you to the following line if there are no mistakes in the code.
  • Step 5: To start your application, type java Hello at the command prompt.
  • Step 6: Your command prompt will output a copy of the Hello world program.

On IntelliJ

writing first program in java

Code Snippet

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

This will be the output

java output

Also read, Why Java is so popular among Developers!

Final Words

The methods written above show that Java can be learned from any platform either online or offline as it is available throughout the internet. The installation of JDK is a long program process but the installation of the compiler is easier and to make it easier I used all the screenshots needed. Hello World! is the first program for every beginner and is easy to run, this gives a basic idea to beginners, of how programming works. But this is just a start you have a lot to learn and you will be enjoying Java while learning with me.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *