Beginner Introduction to Java — the “Programming Language of 2015”

 In the last article, I have highlighted the TIOBE Index’s positions, where Java got the 1st position. Believing the importance of the aforementioned measure of the programming languages popularity and the fact that I promised the readers to focus more on the Java this year, I have decided to write an introductory tutorial on the Java programming language. The whole purpose of this tutorial is to have a friendly easy-to-read guide that can be later referenced by the experienced programmers to their friends who have recently started programming, so they would be reading this guide without resorting to a complex introduction.

Java was developed specifically for the object-oriented programming language. To enforce this paradigm, the Java developers made sure to not support any feature that usually resides in other programming paradigms (i.e., procedural, functional, etc.), thus discouraging the use of other programming styles. It was originally developed by James Gosling when he worked at Sun Microsystems (which is now owned by the Oracle Corporation). It was released as the core component of the Java platform. Before its release, C++ was very popular due its then recently introduced object-oriented features (i.e., classes, inheritance, etc.). Java was specifically designed for running on a virtual-machine, because the developers thought that the code written in it should be compatible to every platform and not just a specific platform where the code is originally written. In C++, when you compile a code, then the executable file can only be run in a particular format where it was compiled. Now, let’s talk about the Java Virtual Machine (JVM).

What is Java Virtual Machine? (JVM)

JVM is a virtual machine that was designed to run the Java bytecode — the instructions that are translated from the Java code by the Java compiler — in the platform where it is installed on the drive. The motivation for its construction is to support the cross-compatibility between different platforms (i.e., Windows, Linux, MacOS, etc.). The arrival of JVM made the language both a compiled language and an interpreted language, because: 1) The Java code (mostly written in .java format) is first “compiled” by the Java compiler into the Java bytecode (mostly written in .class format), then 2) The JVM interprets the bytecode (by translating it to the machine instructions of the platform) to run the program. Thus, this is a two-step process when you desire to compile / run a program written in Java.

Installing the Java Platform

Before getting your hands dirty, you need to first install the Java Platform (JDK + JRE) in order to compile / run the program written in Java.

Under Windows

 By following the instructions below, you would be able to install the Java platform on Windows:
  1. Go to the site to download Java SDK.
  2. Install the SDK once the download is completed.
  3. Go to the “Properties” of “Computer” or “My Computer”, then go to the “Environmental Variables” and then “Advanced System Settings”
  4. Edit the “Path” by adding the destination of the folder where the SDK is installed to the variable value (see the image below).
  5. Open the “cmd” and type “java” to test the installation.
downloading-the-sdk
Downloading the SDK
environmental-variable
Environmental Variables
testing-installation
Testing the installation through “cmd”

Under Linux

Most Linux distributions come with Java pre-installed, so you simply need to open the terminal of your distribution, and write “java” to test the installation. In case it isn’t installed, then you can install by running the following command:
Now, let’s write a “Hello world” program in Java.

Hello Java!

Ok, I cheated: We are going to write a “Hello Java!” program instead of the traditional “Hello World” program! In Java, we need to define a class in order to do anything. Following is the code of the Java program that simply prints “Hello Java!” to the console:

Here, I defined a (public) class “Main” which contains “main” method which has the code to print “Hello Java!”. This code should be saved in a file “Main.java”, because the name of the public class and the file name (excluding .java) should be same according to the syntax rules.Once you have saved the code in “Main.java”, then you need to open command line from the directory of the source file (or change the directory of your command line to the required directory) and then run the following command to compile the code:

Here, the “javac” is a tool to compile the source code. Once the source code is compiled, it creates a Java bytecode file (with .class format) which is called a class of its source file. To run the class file, you use “java” tool (note that I omitted the .class). This will print “Hello Java!” to the console screen:

Beyond Hello Java!

Now that you’ve learned the basic “Hello Java!” program, you can read the other tutorials to learn more about this awesome language! If you find it cumbersome to compile / run Java code through command-line, then you can use the IDE (Integrated Development Environment) for Java.

Summary

This article was an introductory tutorial on the Java programming language. Consider it a good start into the complex field of Java programming language where you need to learn / study at every stages!

If you have any question, then you can ask in the comments section below!

Leave a Reply

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