,

First Java Programming


We have learned what Java is and how it works. Now is the time to dive into Java programming.

I am expecting that you have the latest JDK installed on your system. Let’s start with checking the versions. First, we will check the Java Compiler(javac) version, then the Java Runtime(java) version

As you can see, I have Open JDK 14 installed on my system.

Let’s start with writing our first code. Like we always do, we will start with Hello World

This is how a basic Java code looks. It has a few main parts

  • File Name – If you see, we are writing the code in a file named HelloWorld.java . HelloWorld is the file name, which can vary. But we normally keep the file name and class name the same, matching the class functionality. Also, we follow Camel Case naming convention. For any Java file, we have to use the .java extension
  • Class Name – Next, we have defined a new class with the name HelloWorld, having a public access identifier and class keyword. We will learn more about Java keywords and access identifiers in more detail
  • main method – Every Java program needs to have an entry point, from where execution will start. main is a method that is a keyword and used as an entry point for any Java program. It has a specific signature that needs to be followed. public static are access specifier, void is the return type, String[] args are parameters. We will know about these in more detail
public static void main(String[] args){ }
  • statement below is a Java method that writes “Hello World!” to the screen. This is the only thing our program is doing. We will know about Java methods in more detail
System.out.println("Hello World!");

Code writing is complete. Now, let’s compile and run the code

  • For compilation, we use the javac file name along with the .java extension. For us, it’s javac HelloWorld.java
  • for execution, we use the java class name. For us, it’s java HelloWorld. We have the same class name as the file name, so it’s the same. But in your case, if the file name and class name are different, use the names properly

If you remember how Java works, you should remember about bytecode, an intermediate, platform-independent code generated after Java class compilation using the javac command. Here, HelloWorld.class is the file containing the bytecode. When we run a Java program using the java command, it uses the bytecode from the .class file and executes the code inside the JRE

And that’s how we write a basic Java program, then compile and run it.