Java: Exploring the Programming Language - Key Components, Setup, and Your First Code

Shreyas GosaviShreyas Gosavi
9 min read

Introduction

The world of software development is enriched with many programming languages, thanks to their creators, that help us instruct computers and develop applications to solve real-world problems. Java is one of them, a high-level programming-language that was developed by Sun-Microsystems in 1995 which was later acquired by Oracle and still are its maintainers. Java is famous for its verbose-syntax compared to other languages, but it offers a systematic code-structure that enhances readability.

Java is ranked among the top five programming language in the index (at the time of writing this blog).

Reference:

Key Features Of Java:

  • Object Oriented Nature

  • Platform independent

  • Automatic Memory-Management Feature (garbage collection)

  • Better Security

  • Rich in-built libraries.

It's fine if you don't understand most of its feature as we have just started understanding java. For now, just remember that Java is platform-independent, meaning its compiled code can run on any machine regardless of the underlying OS, making it highly versatile.

As java has rich in-built libraries that helps in development of several types of application like :

  • Desktop Application

  • Mobile Application

  • Web Application

  • Enterprise Application

  • Gaming

  • Many more..

Java has wide application and is in great demand.

Sound's interesting right ?? Let's see more about java and by the end of reading this blog you will be able to set-up Java in your system and run your first-program ๐Ÿ˜Ž.

Why was Java developed?

Java was released in 1995 but before that languages like C, Fortran, C++ already existed then why there was a need to develop another programming-language ?

Java was created to address the shortcomings and limitations observed in other languages, so it was developed from the ground up with these considerations in mind. Another major revolution was the expansion of the internet, so there was a need for software that could work with networked-environment.

Java introduced new features and capabilities that were well-suited for the emerging needs of the software industry, particularly in areas like platform independence, safety, networking, and security.

How is it Platform Independent ?

Until now we have been praising Java's striking feature of platform-independence, but what does it exactly mean for a system to be platform-independent and why was it needed? Let's explore this by first looking at what other system offered, their drawbacks and how this heroic feature of java addresses those issues.

Yes I can follow the given instructions:

We write code in high-level programming language that is human-readable and is simpler to work with as compared to low-level programming language like Assembly language which interact closely with hardware. Computer-system don't understand these high-level programming language, so we need to convert them into machine language that it understands.

Compiler is a computer-program that translates code from one language to the other language, specifically high-level language to low-level language that the machine can understand.

Machine code is directly executable by the CPU, but one drawback is that the generated machine code is tightly coupled with the system. Each operating system has its own architecture, libraries that the compiler must consider when generating machine code. So, different executable code needs to be generated for machines with different underlying operating systems.

Same-Same, But Different Meme. Here it refers to the high-level code that is same but the executable code needs to ne different for different OS's

Write-Once-Run-Anywhere :

Java's coding process differs by adding an additional step between compiling and execution. After compiling the code with javac, Java converts high-level code into an intermediate form called bytecode, which isn't the final executable code.

Java code is executed in a separate Virtual Machine (JVM), which provides the execution environment for running Java programs. Here, the bytecode generated by the compiler is converted into machine code and is interpreted (executed). The JVM allows bytecode generated on any machine to run on any host machine, a feature known as 'Write Once, Run Anywhere' which makes Java Platform-Independent...

Setting Up Java

Now that we have a grasp of Java and its features, it's time to set it up on our system and get it running. But before we jump into the installation process, let's explore the Java ecosystem. This will help prevent any confusion later on.

Java Ecosystem

The Java ecosystem includes the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine(JVM) , covering the entire software development life-cycle, from writing, compiling, debugging and providing runtime for program execution.

Let's understand those components.

Shorter Explanation :

JDK : It is a tool-kit which provides tools like compiler, debugger, documentation and JRE.

JRE : It provides the resources necessary to run java application like the java-class files that the code uses, security settings and includes JVM.

JVM : Responsible for executing code by converting bytecode into machine code.

This is how all 3 look together :

Image Credits : https://www.shiksha.com/online-courses/articles/difference-between-jdk-jre-and-jvm/

Java Development Kit :

  • JDK provides the necessary tools required for development like compiler, debugger, standard-library which consists of code that can be used to perform common tasks.

  • It consists of the Java-API documentation which provides detailed explanation of java-concepts and also provides details about java-api so it can be effectively used.

  • It includes JRE which is the execution environment for running java-application.

Java Runtime Environment:

  • JRE is the execution environment which is used for running the java application in the system.

  • It includes everything you need to run Java programs. It has the necessary files, like libraries and tools, that help your Java applications work properly on your computer.

  • JRE ensures platform independence by providing a consistent runtime environment across different operating systems and devices.

  • It further includes JVM which executes the java code.

Java Virtual Machine

  • JVM is responsible for executing Java programs.

  • It includes tools required for execution at runtime like class loader, bytecode verifier, interpreter, and Just-In-Time (JIT) compiler.

  • It takes care of the memory-management, handling exceptions, and providing other runtime services required by Java applications.

  • Abstracts underlying hardware and operating system

That was short-brief about JRE, JDK and JVM

Software Installation and Setup

Setting-up Java in your system is easy and includes :

  • Downloading software (JDK)

  • Setting System's path-variable

  • Verifying java installation by checking its version

That's it then we can start writing our first java code.....

  1. Downloading Software :

    In the Java-Ecosystem section we have seen that JDK provide us the tools for writing and executing Java-program, so let's download the JDK software. There are several options from where we can download JDK, we can download it from Oracle or from any other Open-Source option like OpenJDK.

    Here we will be downloading it from Oracle,

    link : https://www.oracle.com/middleeast/java/technologies/downloads/

I have downloaded the MSI Installer :

Then install the software :


Note : Keep a note of the installation path


That's it you have successfully installed Java in your system.. Congratulations ๐Ÿฅณ๐Ÿฅณ๐Ÿฅ‚!!

But now how to verify the installation.

Open Command-prompt and go to the directory where java is installed. Now go to its bin folder.

Now type the command java -version, this command should display it's version.

Hurrayy!!!, we have verified the installation and are able to see its version.

But what if we try to run the command outside the Java directory ?

Let's check in C:\ :

That's awesome!!! we can use java command anywhere. But How ? During the installation process 'Oracle' folder is created in path 'C:\Program Files\Common Files' and adds commonly used java-programs in 'C:\Program Files\Common Files\Oracle\Java\javapath'

This path, 'C:\Program Files\Common Files\Oracle\Java\javapath', is added to the Windows path variable, enabling access to Java tools from any location in the system.

However, in some cases, the path variable is not set, resulting in the inability to access Java beyond its installation directory. In that case we need to set path-variable manually.

  1. Setting System's path-variable :

Sometimes system can't identify Java tools like java and might throw an error of not recognizing the command.

In this case we need to add Java's path to the System's path variable. For windows follow the steps :

  • Copy the path where you have installed JAVA.

  • Save the path with variable name JAVA_HOME.

  • You can either add it as Systems variable or variable for particular user.

  • Then add %JAVA_HOME\bin to PATH variable.

  • Save the path-variable then open new Command-prompt and check for the version using java -version .

  • Advantage with this is that we can access other tools of Java anywhere like keytool in future which was not there in the Common Files folder.

If you are curious from where the system is able to execute the command just use where command in Windows.

  1. Verifying the version :

    We have already seen how to verify the installed Java version.

Java is successfully installed and verified !!! Now its time to write our first java-code. Come Onn!!!!!

Writing First Java Code

For now we are going to write our first-java code and execute it and will not cover how java executes the code behind-the-scenes.

Use text-editor of your choice like notepad, create a new file and name it FirstProgram.java don't miss the .java extension. As mentioned earlier Java is known for its verbose syntax, so we need to write some lines of code to make it work.

Simply copy the code and paste it in your newly created file and save the file:

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

Finally it's time to run the code ๐Ÿ˜‡... follow the steps:

  1. Compile the code using javac followed by the file name (FirstProgram.java)

  2. The above command will generate a file with .class extension.

  3. Then run the code using java command followed by just the name of that class file.

Output in green-circle.

Let's understand the code briefly.

  • class FirstProgram : Java code is present inside a class which is written by writing class keyword followed by its name ( FirstProgram ).

  • { : This marks the beginning of the class block.

  • public static void main(String args[]) { :

    The sets of instructions to be executed is written inside a method. Method in java contains the code to execute.

    For now just remember this format : public static void main ( String arg[])

    and it is called method-signature.

  • System.out.println("HEEEEEE") : Prints the content inside the () on the screen.

Amazing! Weโ€™ve just scratched the surface of the Java world by exploring why Java was developed, understanding key concepts like JRE, JDK, and JVM, setting it up on our system, and even writing our first piece of code.

This is a great start! Wishing you all the best as you continue your learning journey!!! ๐Ÿ˜Š

1
Subscribe to my newsletter

Read articles from Shreyas Gosavi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shreyas Gosavi
Shreyas Gosavi