Introduction of Java
Java is a class-based object-oriented simple programming language. Though we can not consider it to be fully object-oriented as it supports primitive datatypes. It is a general-purpose, high-level programming language that helps programmers and developers to write a code once and run it anywhere.
Java is considered both a compiled and interpreted language. It is because Java source code is first compiled to bytecode which is then interpreted by Java Virtual Machine. Java Virtual Machine interprets the bytecode and converts it to platform specific machine code. Hence, Java is also called a platform-independent programming language.
What is Java Programming?
Java is a programming language that James Gosling developed at Sun Microsystems_Inc in the year 1995, which later on was taken into possession by the Oracle Corporation in 2009.
- We can call it a high-level (makes the development of programs easy and much more user-friendly) programming language which makes it very convenient for us to write, compile and debug Java programs.
- Java is a class-based object-oriented programming language that implements the principle of write once code anywhere.
- Since Java applications are compiled to byte-code, they can run on any JVM-supported machine.
- Java codes are very similar to C/C++, which makes them easier to understand.
Let’s further look into this and read a few more facts about Java.
Why Java Programming Language is Called JAVA?
Java is the name of a beautiful island in Indonesia. It is also said that the first coffee was named right here after java coffee. The name was chosen by James Gosling during the daytime when he was enjoying a cup of coffee near his office. Java was initially called by the name: OAK.
However, in the wake of the Oak Technologies, the team had officially decided to rename it. The options they considered were Silk, Revolutionary, Dynamic, DNA, and Jolt. Even though Silk was further selected, they decided to go along with Java as it was unique, and a lot of people preferred it.
Now let's move on to why we use it.
Why Use Java?
As mentioned above, Java is a high-level language and should be studied if you want to know the basics of programming. Let us learn why there is a need to learn the language.
6) Platform Independent
7) Architecture neutral
All of these features will be explained in detail below.
Before moving further, there are some Java terms that we need to understand. So let's have a brief understanding of the Java terminology.
Java Development Kit (JDK)
As the name formally states, Java Development Kit is a full-time kit that has a compiler, Java Runtime Environment(JRE), Debuggers, and Java documents included in it. For further execution in java, we need to have JDK installed on our computers to further lead to the creation, compilation, and running of the java program.
Here, as we use JDK, we need an environment to run the programs. We use JRE Java Runtime Environment, which provides the least requirements to execute the Java program. It provides the JVM, Core classes, and supporting files.
Java Run-time Environment (JRE)
JDK includes JRE, which, in turn, after installation, allows the Java program to run. But we still can't compile it. It has a browser, the applet supports, and a few plugins included in it. So, to run a java program on your respective computers, you need JRE.
JRE is made up of multiple elements altogether, and they are:
- Java virtual machine (JVM)
- Java class libraries
- Java class loader
When our software tends to execute a particular program, it requires some environment to run in. Usually, it's any operating system, for example, Unix, Linux, Microsoft Windows, or macOS. Here JRE acts as a translater and facilitator between the java program and the operating system.
Java Virtual Machine (JVM)
This generally is referred to as JVM and contains three phases that we have to follow. It is a very important part not only of JDK but also JRE as it is inbuilt in both of the places. When you run a program using the JRE and JDK, it also goes to the JVM as it is required to run the java program and interprets the program. The phases are as follows:
Compile the Code: The Java Development Kit(JDK) provides us with the JAVAC compiler to get through this step. We basically require the JDK to convert our source code into a specific format (compiled code) that can be easily interpreted by the Java Runtime Environment(JRE).
Run the Code: JVM runs the bytecode provided by the compiler. Since Java is a platform-independent language, the compiled code produced by the JAVAC compiler is converted to machine code using platform specific JVMs. Different platforms have different JVMs. JVMs convert the bytecode into platform specific machine code.
Let’s now see how this bytecode is developed, making it easy for us to execute our programs.
Bytecode in the Development Process
The source code provided to the JAVAC compiler that is present in JDK gets compiled into a bytecode that the JVM can execute. It gets saved as a .class file by the compiler itself. We can also view this bytecode using a disassembler like javap.
Within Java, the programmers cannot delete the objects. Hence, to delete or recollect a memory, JVM has a Garbage Collector. These Garbage Collectors can recollect those objects which have not been referenced. Using this Java makes the life of a programmer very convenient by managing the memory. Despite this, programmers should be aware of what they are writing in their codes or if they are using objects that have already been used for longer. This is because this collector can’t restore the memory of objects that are referenced. Garbage collection here is an automatic process.
Classpath is a particular file path where both the Java runtime and Java compiler look for .class files to appear. Although by default, the Java Development Kit provides us with quite a few libraries. Here you can also include an external set of libraries, but they should be added to the classpath beforehand.
So we are done with the terminology now, so we can easily come and look at Java's features.
Main Features Of Java
This section will explain the features of the Java programming language. Let's have a look at all the main features of the Java programming language.
First and foremost, the compiler converts the source code to its bytecode, and later on, JVM executes the bytecode generated by its compiler. Further, this bytecode can be run on any other platform, for example, Windows, Linux, or macOS. This means that we can compile a program on windows, then we can run it on Linux as well using the Linux's JVM.
Every Operating System has different sets of JVM. These different JVMs generate different platform-specific machine code from the same bytecode. Hence, the output which will appear on all the Operating systems will be the same after the execution of our source code. And, so we call it a Platform Independent language.
Object-Oriented Programming Language
Object Oriented Programming organizes the software design and logic around classes, objects and data rather than procedures or functions. Object Oriented Programming uses real world concepts to implement the application code and keep data security and integrity at the centre of design.
Let's understand this with the help of a real-life example:
We all know that under the Class of Car, we have many options like Alto, WaganR, Santro, etc. In this manner:
- Each Car object has its specific Model, Colour, Engine power, Top Speed, Year of Manufacture, etc., which altogether are called the properties of the Car Class
- The functions performed by it, like Start, Stop, Move, etc., are the methods of this Car Class.
No memory is allocated on the creation of a class, but we allocate memory when the object is created, which in our case is a new car object.
The four basic principles in Object Oriented Programming:
- Inheritance Inheritance can be defined as an object acquiring all the behaviors and properties of a parent object. It makes our code reusable. Runtime polymorphism can be achieved using inheritance.
So here, the voices of everyone, including the animals and the humans, are inherited by their parents; the dog barks like a dog, and the human speaks like his parents. Hence, we can see a real-life example of inheritance.
- When we perform a single task in multiple ways, it can be defined as polymorphism. In Java, programming language method overriding and method overloading is used to attain polymorphism.
- Overloading: when multiple methods in the same class have the same names but different parameters.
- Overriding: when the method name and the parameters are the same in the child class and the superclass, we call it overriding.
Abstraction is the concept of hiding internal implementation details and exposing only the required functionalities to the end users.
In Java programming language, the interface and abstract class are used to attain abstraction.
Example: We all visit the ATM machine for withdrawing cash and several other things as well, but we never get to know what happens internally inside the machine when we insert our ATM card for any operation. This can be seen as abstraction in real life.
- When data is bonded or wrapped together into one single unit, we can call it encapsulation. All the data and methods operating on the data are enclosed in the same unit.
- In the Java programming language, the java class is a major example of encapsulation.
- JavaBean is noticed to be fully encapsulated, and this is because all the data members are observed to be private here.
- The way this capsule encloses all the contents of the medicine in a similar way when we wrap up data members and methods inside a class, we can term it to be encapsulation.
Java is generated in a very convenient manner to be able to understand easily. If you understand the basic OOPs, it is easy to master the art. Moreover, it does not contain any complex features like pointers, multiple inheritances, operator overloading, etc.
So the literal meaning of robust can be easily understood as reliable. Java has been developed in such a way that it checks for errors pretty early, which results in errors being detected by the java compiler, which is pretty difficult when it comes to other programming languages.
Java turns out to be very secure, and that is because we do not have access to pointers here. Due to the lack of pointers, we are unable to get access to outbound arrays, and this makes it impossible for the flaws like buffer overflow or stack corruption to occur.
Having no implementations and being independent of the aspects of specification make Java Portable. It can be run on any platform as it is completely platform-independent like windows Linux macOS. This means that if you compile a program on Linux, you can run it on windows and the other way round.
Java programs run in a separate space that allows user to execute their applications without affecting the underlying system with the help of a bytecode verifier. Bytecode verifier also provides additional security as its role is to check the code for any violation of access.
Architecture neutral (Write Once Run Anywhere)
This can be seen as one of the best features of java as it states that a Java program can be easily executed on any processor irrespective of its vendor and architecture. Java application generates a .class file, which corresponds to our applications but contains code in binary format. It provides architecture-neutral ease as bytecode is not dependent on any machine architecture.
So features of the java programming language were pretty interesting, and now it’s time to see where we can use them. So, let’s see some applications of the Java programming language.
Applications of Java Programming Language
As Java kept getting better and more advanced, it became pretty popular, building several configurations capable of suiting multiple platforms.
Java is famous for having the write once, run everywhere property. It also has new J2 versions, renamed Java SE, Java ME, and Java EE, respectively.
Given below is a list of applications of the java programming language. Let’s have a look:
Using this feature, it becomes easy for us to write programs that are able enough to perform multiple tasks simultaneously. This comes in handy to developers when they make interactive applications that can run smoothly.
The Java architecture is designed in such a way that it reduces the overhead when we run an application, and at some times, it uses the JIT(Java In Time) compiler where the compiler easily compiles the code on-demand basics where it only compiles the methods that we call which makes the applications execute faster.
When we use the Java programming language, we get able enough to create distributed applications. RMI(Remote Method Invocation) and EJB(Enterprise Java Beans) help us to create distributed applications. Let’s understand in simple words: The language can easily be distributed onto more than one system which is connected to each other through an internet connection. Objects on one JVM can be easily used to execute the procedures on a remote JVM.
Java can be easily considered to be more dynamic than the C and C++ programming languages. This can be said by looking at its ability to adapt to an evolving environment. The programs are capable of carrying an extensive amount of run-time information, and this information can be used in the verification of objects during the run-time.
Simple Program in Java
Let's have a deeper understanding of how a java program works.
1. // - This symbol in the code represents Comments in Java. The purpose of comments in a program or code is to explain the program. When you compile a program, the compiler ignores the comment lines and are not executed. There are two types of comments single-line and multi-line. Let's look at the syntax of single-line and multi-line comments.
2. import java.io.- Import is a keyword that includes the class we use in the code. This is used to import all the classes of the io package. Java's io package includes a set of input and output streams used to read or write files and input or output.
3. public class-The data and methods that need to be used in the program are included in the class. Methods define the behavior of the class.
4. public static-Static methods do not require objects to get called. They can be executed using the dot operator with the class name.
5. void- Does not return any value. It's an empty void.
6. main()- Name of the method. Entry point method where JVM runs the program.
7. (String[ ] args)- These are used as command-line arguments and are passed as strings.
8. System.out.println- This command prints the contents provided within the print statement.
- Java can be a class-based object-oriented programming language designed to minimize implementation dependencies as much as possible.
- Java is a high-level, easily understandable language with syntax similar to C/C++.
- Java is platform-independent, and this makes it better than any other programming language. Java implements the write once run anywhere principle.
- Java is a portable, simple, secure, and robust programming language well-suited for large codebases.
- The JDK is a development environment for building applications, applets, and components using the Java programming language.
- The JDK includes tools useful for developing and testing programs written in the Java programming language and running on the Java platform.
- JRE is made up of multiple elements altogether, and they are: Java virtual machine (JVM), Java class libraries, and Java class loader.
- Both JDK and JRE require JVM, which is specifically responsible for converting bytecode into machine-specific code.
- JVM is platform-dependent; it carries out a variety of tasks, such as memory management. In addition, JVM may execute programs that have been converted to Java bytecode from other programming languages.
- Garbage collector in Java is used to recollect those objects which can not be referenced anymore.
- Classpath is a particular file path where both the Java runtime and Java compiler look for .class files to appear.
- Java is used in Multithreaded, distributed, dynamic, and high-performance applications.