StackTips

Introduction to Java, JVM and Writing Your Fist Java Program

manas avtar

Written by

Manas,  11 min read,  2.75K views, updated on Sept. 17, 2023

Java is a high-level English like an object-oriented programming language with tons of APIS (application programming interface) for developing applications. Java is a platform independent language with support for performing complex computation, graphics, server and mobile application development.

Java language syntax looks pretty much like C and C++, however, there are some differences such as the absence of pointers, allows writing object-oriented code. Learning Java is easy for a developer with a basic C programming experience.

Java is one of the vastly used languages as it supports “write once run anywhere” architecture. A java program written in Macintosh can be executed on Windows or Linux without any modification or without recompiling it again. To understand how Java achieves platform independence, we must understand the way the java program is compiled and executed.

Writing First Java Program

A simple java program can be written in any text editor such as Notepad, WordPad, notepad++ or edit plus. While writing your Java program make sure it must have one public class and one public static void methodmain() which allow it to run a java program when invoked in Java Virtual Machine.

For now without worrying much about how to go with it, just copy and paste the following code in your favorites text editor. We will learn about the building blocks in details on the following sections. After pasting the following code, save your file with name HelloWorld.java extension. Now we are ready for next step to compiling and run Java program.

/* Hello World Java Program */
public class HelloWorld {

  public static void main(String[] args) {
    System.out.println(“Hello World!”);
  }
}

Compiling Java Code

Compiling Java code requires a compiler that comes along with Java Development Kit also called as JDK. This tutorial assumes that you have already installed Java JDK on your development computer, if not you can download and install from Oracle’s site.

If you are not sure, if java is installed properly, you can check by entering the commandjavac –help in your command line. If installed properly, you will see the output as depicted in the screenshot below.

Compiling Java code

Once you are ready with your Java development environment, you can compile your java code by invoking the commandjavac from command line. Enter the following command to compile HelloWorld.java

> javac HelloWorld.java

The above code will compile your java source file and produce a new file named.HelloWorld.class Open HelloWorld.class file in a text editor; notice that it contains some unreadable bytecode. This Class file is actually java’s way to achieve platform independence. This class file contains JVM specific bytecode instead of machine code, unlike the C or C++ that makes it possible to run java program in any platform.

Running Java Application

We know that after compilation java compiler creates a.class file that contains JVM specific bytecodes and to execute those bytecodes we need a piece of software called Java Virtual Machine or JVM. JVM is also known as Java Runtime or JRE. Bytecodes of java programs executed inside Java Virtual Machine which is platform dependence, you here it correctly JVM are indeed platformed dependent and it has the different binary for the different platform which you can download from Sun’s site. JVM also comes with JDK or you can download it separately.

Use the following command to compile the above code

> java HelloWorld

The output of the above java program is
Hello World!

Importance of JVM?

It’s important to know that the JVM ((Java Virtual Machine) doesn’t have any information high-level java code. It knows the only binary format of the bytecode and ensures that the class file generated by java compiler is as per Java Byte Code specification thus eliminating dangers of running a malicious and manipulated java bytecodes.

Since every Java program runs within the boundaries defined by Java Virtual machine or JVM it provides inherent Security. The code run inside the JVM cannot go beyond the security constraints defined by the Java Virtual Machine. This is why java application is considered as secure applications over the internet and Java applets are the most widely used programming platform for the internet. Apart from Security Java Virtual machine also provides memory management via Garbage collection that enables java programmer or Java developer to focus more on business logic rather than worrying about system aspects like allocating and reclaiming memory.

In conclusion with the use of the class file, bytecode and Java virtual machine, Java achieves its platform independence and makes development easy to run across different platform.

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE
manas avtar

Manas

He is a java and web and enterprise application developer in Bangalore area, India. He has expertise in Servlets, JSP, Hibernet, Struts, JavaScript, Groovy, UML and other related tools and technologies.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!