Java Tutorial

Raj Khattar
5 min readSep 22, 2021

Overview of Java Tutorial

Java is one of the most popular programming la0nguages. It was developed by James Gosling in the year 1991, and the purpose was to use it in his set-of-the-box projects. It was initially named as ‘Oak’ which is a tree name that stood outside the Gosling’s office window and also went renamed it as a ‘Green’, but later it ended up with a name known as JAVA.

Sun Microsystems originally developed Java programming language in the year 1995 initiated by James Gosling’s, and it was the first core component system of the Sun Microsystems. Java has got massive popularity among the programming languages due to its flexibility to configure on multiple platforms. For example, J2EE is used for the development of Enterprise applications and J2ME is for mobile applications.

Java Features

#1 Object-Oriented

Object-oriented programming is at the heart of Java. Almost all the Java programs are object-oriented to some extent. Object-oriented programming (OOPs) is a method to simplify software development and maintenance by following some rules.

#2 Platform Independent

Unlike the C compiler, the Java compiler produces and converts the source code into a unique format called bytecode. Bytecode is understandable by any JVM installed on any Operating System. Java’s runtime platform runs on top of the hardware platform of any OS. Due to this, Java source code can run on all OS and hence make Java a platform-independent language.

#3 Secure

Java is declared the most secure programming language. It is secure as its source code runs in a virtual machine sandbox.

#4 Robust

Java focuses on compile-time and runtime error checking thus reducing the error-prone situations.

Are you interested in taking up Core Java Certification Training? Enroll Now for Core Java Training

#5 Portable

Java bytecode is portable. You can carry it to any platform and it will work. It doesn’t require any implementation.

#6 High Performance

Java’s bytecode is close to native code and that is why it is faster than all other programming languages.

#7 Architecture-neutral

Java compiler created an architecture-neutral object file format. This facilitates the compiled code to work on many processors with JRE.

Top 10 Programming Languages that you need to watch out to boost your career in 2021

#8 Multi-threaded

Java enables developers to code in such a manner that multiple tasks can be done simultaneously. This can enhance the user experience of any application due to its interactive nature.

#9 Distributed

Java is designed and developed to work in distributed environments on the internet.

Related Article: Java Interview Questions

How does Java Works?

To know how Java works, we need to understand 3 basic terms: JDK, JRE, and JVM.

JVM: JVM stands for Java Virtual Machine. It does not physically exist hence called a virtual machine. JVM converts bytecode into the language understood by a particular OS and is responsible for executing Java code line by line. It is also termed an interpreter.

JRE: JRE stands for Java Runtime Environment. It provides the runtime environment for executing Java code. It can only execute the existing code. We cannot develop code or build new applications using JRE. It contains a set of library class files and other files which JVM uses at runtime.

JDK: JDK stands for Java Development Kit. It contains JRE and development tools to develop Java applications and build new apples. Developing new applications, modifying existing applications, compiling java code, and executing them is possible through JDK.

Java Prerequisites

For executing Java code, we need below 3 things installed in our machine

  • Linux or Windows Operating System
  • Java Runtime Environment
  • Notepad or any other text editor to write code

Nowadays, we have so many java tools available like Eclipse, Netbeans which makes coding interesting and easy.

Java Environment Setup

Java can be installed from oracle’s official website. Once it is installed, you can verify the version from the command prompt using the command “java -version” like below:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:Userspalakharshadbhai.sha>java -version
java version "1.8.0_171"
Java (TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot (TM) Client VM (build 25.171 -b11, mixed mode)

Once it is installed, we need to set it up in our system to run the java program effortlessly. To set up the Java environment in Windows, follow the below steps.

  • Navigate to System Properties → Environment variables
  • Choose the system variable “Path” and edit it
  • Add a new path to that variable. Give the value of the bin directory of Java installation. The path can be like this — C:Program FilesJavajdk1.8.0_171bin

A Simple Java Program

Let us start by writing a simple Java program.

class Test {
//first sample program
public static void main(String args[]) {
System.out.println("Our first Java program");
}

The file which contains Java source code is called a source file.

Few things to consider while writing Java programs:

  • All the java code must reside in a class
  • Java is a case-sensitive language so the variables v1 and V1 holds different values.
  • The source file name should be the same as the name of the main class
  • A source file can contain multiple classes
  • All statements in Java ends with a semicolon
  • Java compiler needs the source file to have a .java file extension

Compiling the program

To compile the program, we need to pass the filename to the command javac like shown below:

javac Test.java

The javac compiler generated the .class file which is nothing but the bytecode version of the code written in the source file. Java bytecode is the intermediate presentation of the code which is understood by JVM. So, the output of javac cannot be executed directly.

To run the program, we need to pass the class name with the command java.

java Test

This will execute the bytecode and print the output:

Our first Java Program

Now, let us try to decompose the code and understand the keywords used in this code unit.

  • class — class is used to declare the new class. The test is the identifier which is the name of the class.
  • // — A single line of comment is defined by // in Java
  • public — the public is an access modifier that is concerned with class members’ accessibility. public means all class members are visible to all
  • static — To call any method we need an object of a class. But the main method is called by JVM without any objects. This is due to the static keyword used which allows the main method to be called without any object of a class.
  • void — the void is the return type of method. This tells the compiler that the main method is not returning a value.
  • String args[] — this declares a parameter called args which is an array of strings. Any command-line arguments will be stored in this parameter.
  • System.out.println() — This statement is used to print text passed in method println(). The system is a predefined class that gives access to the system. out is the object of PrintStream class and println() is the method of the same class.

--

--