Writing your first java program - Complete guide

Writing Your First Java Code

Hey there. In this blog, I will explain all the steps you need to perform to write your first Java code. First, we need to Install Java from the internet. For that, you can go and search Install JDK On any browser you prefer. 


If you are on Windows, Click here to download JDK on Windows


After installing Java on your computer, You can check if it's installed  by opening the command prompt and typing  java --version

If you see something like that you are good to go. For the error see belowNow go to the Notepad or any text editor and write your first program in Java. If you use any IDE or editor like VS code or IntelliJ IDEA you can go and write your program there. Create a "hello.java" file in the Notepad. 

//CODE


public class hello {

  public static void main(String[] args) {

      System.out.println("Hello, World!");

  }

}


This is the code for writing a simple Hello, World!in Java. At first, it may look scary to you, but with time you will get used to this. 

Let us discuss the various parts of the code, one by one:

public class hello 

As we know Java is an object-oriented programming language. Don't worry you never heard this term. There are many types of programming languages in the market. Such as procedural programming language, scripting language, object-oriented programming, functional programming language, and many others. Java codes are written in classes and objects. So every Java program should at least contain one class named the same as your filename. So here, we used hello as our filename. The public keyword is an access modifier that determines the visibility or accessibility of a class, method, or variable. We'll discuss this in later posts. 

public static void main(String[] args) 

static -keyword makes the method associated with the class rather than an object. 

Meaning: You don’t need to create an instance of the class to call the main method. As I told you earlier, everything in java works in classed and object, so when we define a method static that means it can be called without creating an object of that class. 

main -This is the method the JVM looks for as the starting point of a Java program. JVM stands for Java Virtual Machine. It's responsible for running Java programs by interpreting or compiling the bytecode (the compiled form of Java source code). 

void - It's the return type of the method. So void means that method returns nothing. 

String[] args -It's the parameter of the main method. Suppose if you are using command line for running the java program then args holds command-line arguments() passed when running the program. 

System.out.println("Hello, World!"); - This is used to print Hello, World! On the screen. 

The semicolon(;) is used to terminate the statement. Semicolons make it easier for the compiler to parse the code. Without them, the compiler wouldn’t understand the boundaries of each statement.

After following this, you may encounter errors like path not recognized while running java --version. In that case, you need to act like a software engineer and fine the solution. Just kidding,

Solution for 'Path Not Recognized' Error

  • Find you java folder and copy that C:\Program Files\Java\jdk-23\bin (It should look like that)
  • Search for "Edit the environment variable" In the search bar.
  • Now click on environment variable which can be seen in the bottom. 
  • After that, in the system variables, choose Path and click on Edit.
  • Click on New and paste the Path you copied in the first step.
  • Click OK, and now you are ready to run your first program. 

Running the program:

  1. Open your CMD(Command prompt) in the same folder where you saved you hello.java file. 
  2. Type javac hello.java
  3. Now type java hello
  4. Here you can see "Hello, World!" on your command prompt. 
Thank you for reading.



Post a Comment

0 Comments