Introduction To Java

Java is an object-oriented, class-based programming language. The language is designed to have as few dependencies implementations as possible. The intention of using this language is to give relief to the developers from writing codes for every platform. The term WORA, write once and run everywhere is often associated with this language. It means whenever we compile a Java code, we get the byte code (.class file), and that can be executed (without compiling it again) on different platforms provided they support Java. In the year 1995, Java language was developed. It is mainly used to develop web, desktop, and mobile devices. The Java language is known for its robustness, security, and simplicity features. That is designed to have as few implementation dependencies as possible.

History

The Java language has a very interesting history. Patrick Naughton, Mike Sheridan, and Jame Gosling, known as the Green team, started the development of Java in the year 1991. These people were the engineers at Sun Microsystems. In 1996, the first public implementation was released as Java 1.0. The compiler of Java 1.0 was rewritten by Arthur Van Hoff to comply strictly with its specification. With the introduction of Java 2, the new versions have multiple different configurations that have been built for the various platforms. It is worth noting that James Gosling is also known as the father of Java.

The ISO standard body was approached by Sun Microsystems in the year 1997 to formalize Java, but the process was withdrawn soon. At one point in time, Sun Microsystems provided most of its implementation of Java available without any cost, despite having the status of proprietary software.

Application Programs

The Implementation of an application program in Java application includes the following steps.

  1. The program creation (writing the code)
  2. The program compilation.
  3. Executing the compiled code.

It is worth noting here that JDK (Java Development Kit) should be installed properly on the system, and the path should also be set.

The program Creation

The Java program can be written using a Text Editor (Notepad++ or NotePad or other editors will also do the job.) or IDE (Eclipse, NetBeans, etc.).

FileName: TestClass.java

Write the above code and save the file with the name TestClass. The file should have the .java extension.

The program Compilation

Open the command prompt, and type javac TestClass.java. javac is the command that makes the Java compiler come to action to compile the Java program. After the command, we must put the name of the file that needs to be compiled. In our case, it is TestClass.java. After typing, press the enter button. If everything goes well, a TestClass.class file will be generated that contains the byte code. If there is some error in the program, the compiler will point it out, and TestClass.class will not be created.

Running / Executing the program

After the .class file is created, type java TestClass to run the program. The output of the program will be shown on the console, which is mentioned below.

Output:

Hello World is my first Java Program.

The Name Java

Initially, the name oak was given to the language. However, the team decided that a new name should be given to the language, and words like, DNA, revolutionary, Jolt, Dynamic, Silk, etc. were suggested. All these names were fun to say and easy to spell out. But what was missing was the essence of the language in the suggested names, which the team wanted to have. As per James Gosling, Java, and Silk were two of the most popular options, and since Java had a unique name, most people preferred it.

Java is also a name of an island in Indonesia where coffee (named Java Coffee) was produced. The name Java was chosen by James Gosling because he was having coffee near his office. Readers should note that Java is not an acronym. It is just a name.

The Terminologies in Java

JVM (Java Virtual Machine): JVM is the specification that facilitates the runtime environment in which the execution of the Java bytecode takes place. Whenever one uses the command java, an instance of the JVM is created. JVM facilitates the definition of the memory area, register set, class file format, and fatal error reporting. Note that the JVM is platform dependent.

Byte Code: It has already been discussed in the introductory part that the Java compiler compiles the Java code to generate the .class file or the byte code. One has to use the javac command to invoke the Java compiler.

Java Development Kit (JDK): It is the complete Java Development Kit that encompasses everything, including JRE(Java Runtime Environment), compiler, java docs, debuggers, etc. JDK must be installed on the computer for the creation, compilation, and execution of a Java program.

Java Runtime Environment (JRE): JRE is part of the JDK. If a system has only JRE installed, then the user can only run the program. In other words, only the java command works. The compilation of a Java program will not be possible (the javac command will not work).

Garbage Collector: Programmers are not able to delete objects in Java. In order to do so, JVM has a program known as Garbage Collector. Garbage Collectors recollect or delete unreferenced objects. Garbage Collector makes the life of a developer/ programmer easy as they do not have to worry about memory management.

Classpath: As the name suggests, classpath is the path where the Java compiler and the Java runtime search the .class file to load. Many inbuilt libraries are provided by the JDK. However, if someone wants to use the external libraries, it should be added to the classpath.

Salient Features of Java

Platform Independent: Instead of directly generating the .exe file, Java compiler converts the Java code to byte code, and this byte code can be executed on different platforms without any issue, which makes Java a platform-independent language. Note that in order to execute the byte code, JVM has to be installed on the system, which is platform dependent.

Object-Oriented Programming Language: The concept of object-oriented programing is based on the concept of objects and classes. Also, there are several qualities that are present in object-oriented programming. A few of them are mentioned below.

  • Abstraction
  • Inheritance
  • Polymorphism
  • Encapsulation

The Java language also extensively uses the concepts of classes and objects. Also, all these features mentioned above are there in Java, which makes Java an object-oriented programming language. Note that Java is an object-oriented programming language but not 100% object-oriented.

Simple: Java is considered a simple language because it does not have the concept of pointers, multiple inheritances, explicit memory allocation, or operator overloading.

Robust:

Java language is very much robust. The meaning of robust is reliable. The Java language is developed in such a fashion that a lot of error checking is done as early as possible. It is because of this reason that this language can identify those errors that are difficult to identify in other programming languages. Exception Handling, garbage collections, and memory allocation are the features that make Java robust.

Secure: There are several errors like buffer overflow or stack corruption that are not possible to exploit in the Java language. We know that the Java language does not have pointers. Therefore, it is not possible to have access to out-of-bound arrays. If someone tries to do so, ArrayIndexOutofBound Exception is raised. Also, the execution of the Java programs happens in an environment that is completely independent of the Operating System, which makes this language even more secure.

Distributed: Distributed applications can be created with the help of the Java language. Enterprise Java beans and Remote Method Invocation are used for creating distributed applications. The distribution of Java programs can happen easily between one or more systems that are connected to each other using the internet.

Multithreading: The Java language supports multithreading. The multithreading feature supports the execution of two or more parts of the program concurrently. Thus, the utilization of the CPU is maximized.

Portability: We know that Java is a platform-independent language. Thus, the byte code generated on one system can be taken on any other platform for execution, which makes Java portable.

High-Level Performance: The architecture of Java is created in such a fashion that it decreases runtime overhead. In some places, Java uses JIT (Just In Time) compiler when the code is compiled on a demand basis, where the compiler is only compiling those methods that are invoked and thus making the faster execution of applications.

Dynamic Flexibility: The Java language follows the Object-Oriented programming paradigm, which gives us the liberty to add new methods and classes to the existing classes. The Java language also supports functions mentioned in C/C++ languages and which are generally referred to as the native methods.

SandBox Execution: It is a known fact that Java programs are executed in different environment, which gives liberty to users to execute their own applications without impacting the underlying system using the bytecode verifier. The Bytecode verifier also gives extra security as it checks the code for the violation of access.

Write Once Run Anywhere: The Java code is compiled by the compiler to get the .class file or the byte code, which is completely independent of any machine architecture.

Compiled and Interpreted Language: Most of languages are either interpreted language or the compiled language. However, in the case of the Java language, it is compiled as well as the interpreted language. The Java code is compiled to get the bytecode, and the bytecode is interpreted by the software-based interpreter.

A Few Programs on Java

A few basic Java programs are mentioned below.

Program - 1

FileName: DemoClass.java

Output:

Welcome to javaTpoint.

Program - 2

FileName: AddMul.java

Output:

Sum = 26
Product = 168

Program - 3

FileName: ComputeAv.java

Output:

Input the First Number: 
4
Input the Second Number: 
5
Average = 4.5

Program - 4

FileName: FahrenheitCelsius.java

Output:

Input the temperature in Celsius:
15
Temperature in Fahrenheit is: 59.0

Program - 5

FileName: TriangleArea.java

Output:

Enter the value of triangle's three sides:
15
16
17
Triangles Area: 109.98181667894016

Description Of Various Keywords Used in the Above Programs

//: It is used for putting comments in the code to make it more readable for the readers. The compiler ignores the comments completely while compiling the program. For multi lines comment, we use: /* … */

main() method: The most important method of the program where the execution begins. Therefore, all the logic must reside in the main method. If the main() method is not containing the logic, then it will be there in some other method, but that method must be invoked from the main() method directly or indirectly.

class: The keyword class is used for declaring class in the Java language.

void: it means that the function or method will not be returning anything.

System.out.println(): It is used to print statements, patterns, etc., on the console.

String argvs[]: It is a command line argument that is used for taking input.

public: It is an access specifier keyword. When it is applied to a method, then that method is visible to all. Other access specifier keywords are, private, protected, and default.

import java.io.*: It means that all of the classes present in the package java.io is imported. The java.io package facilitates the output and input streams for writing and reading data to files. * means all. If one wants to import only a specific class, then replace the * with the name of the class.

System.in: It is the input stream that is utilized for reading characters from the input-giving device, which is usually a keyboard in our case.

static void main(): The static keyword tells that the method can be accessed without doing the instantiation of the class.

System.out: As System.in is used for reading the characters, System.out is used to give the result of the program on an output device such as the computer screen.

double, int: The different data types, int for the integers, double for double. Other data types are char, boolean, float, etc.

println(): The method shows the texts on the console. The method prints the text to the screen and then moves to the next line. For the next line, ln is used. If we do not want the cursor to move to the next line, use the method print().