Introduction of Java
Java is a class-based object-oriented simple programming language. However, 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 code once and run it anywhere.
Java is considered both a compiled and interpreted language. This is because Java source code is first compiled to bytecode, which is then interpreted by the Java Virtual Machine. The 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 1995. In 2009, Oracle Corporation acquired it.
- We can call it a high-level programming language (which makes program development easy and much more user-friendly), 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.
- Java applications can run on any JVM-supported machine since they are compiled to byte-code.
- Java codes are very similar to C/C++, which makes them easier to understand.
Let’s look into this further 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. ** James Gosling ** chose the name 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 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 many 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.
1) Object-Oriented
2) Portable
3) Simple
4) Secure
5) Robust
6) Platform Independent
7) Architecture neutral
All of these features will be explained in detail below.
JAVA Terminology
Before proceeding, we need to understand some Java terms. So, let's have a brief understanding of the Java terminology.
Java Development Kit (JDK)
As the name formally states, the Java Development Kit is a full-time kit that includes a compiler, Java Runtime Environment(JRE), Debuggers, and Java documents. 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, allows the Java program to run after installation. But we still can't compile it. It has a browser, applet support, 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 an environment to run in. Usually, this environment is any operating system, such as 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 is generally referred to as JVM and contains three phases that we have to follow. It is a very important part of both JDK and JRE as it is built in both 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 interpret 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.
Garbage Collector
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.
Class Path
Classpath is a particular file path where both the Java runtime and Java compiler look for .class files to appear. However, 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.
We are done with the terminology now, so we can easily examine 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.
Platform Independent
First and foremost, the compiler converts the source code to bytecode, and later, the 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 and then run it on Linux using Linux's JVM.
Every Operating System has different sets of JVMs. These different JVMs generate different platform-specific machine code from the same bytecode. Hence, the output that appears on all 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 keeps 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 it performs, such as Start, Stop, Move, etc., are the methods of this Car Class.
No memory is allocated when a class is created, but we allocate memory when an 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 behaviours 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.
-
Polymorphism
- 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
-
Abstraction is 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 to withdraw cash and for several other reasons, but we never know what happens internally inside the machine when we insert our ATM card for any operation. This can be seen as an abstraction in real life.
-
-
Encapsulation
- When data is bonded or wrapped 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 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.
Simple
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.
Robust Language
So the literal meaning of robust can be easily understood as reliable. Java has been developed so that it checks for errors pretty early, which results in errors being detected by the java compiler, which is pretty difficult for other programming languages.
Secure
Java turns out to be very secure because we do not have access to pointers here. Due to the lack of pointers, we are unable to access outbound arrays, making it impossible for flaws like buffer overflow or stack corruption to occur.
Portable
Java is portable because it has no implementations and is independent of specification aspects. It can be run on any platform as it is completely platform-independent, like Windows, Linux, and MacOS. This means that if you compile a program on Linux, you can run it on Windows, and the other way around.
Sandbox Execution
Java programs run in a separate space, which allows users to execute their applications without affecting the underlying system with the help of a bytecode verifier. It also provides additional security.
Architecture neutral (Write Once Run Anywhere)
This can be seen as one of Java's best features, as it states that a Java program can be easily executed on any processor irrespective of its vendor and architecture. Java applications generate 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 the java programming language features 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.
Below is a list of applications of the Java programming language. Let’s have a look:
Multithreaded
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.
High Performance
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.
Distributed
When we use the Java programming language, we are able to create distributed applications. RMI(Remote Method Invocation) and EJB(Enterprise Java Beans) help us create distributed applications. Let’s understand in simple words: The language can easily be distributed onto more than one system connected to each other through an internet connection. Objects on one JVM can be easily used to execute procedures on a remote JVM.
Dynamic
Java can easily be considered 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, which can be used to verify objects during run-time.
Simple Program in Java
Let's have a deeper understanding of how a java program works.
Output
Explanation
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 class includes the data and methods that need to be used in the program. The methods define the class's behavior.
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. The entry point method is 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.
Conclusion
- 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, making 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 Java Development Kit (JDK) provides essential tools for creating, testing, and deploying software written in Java, designed to function seamlessly within the Java ecosystem.
- JRE is made up of multiple elements altogether: Java virtual machine (JVM), Java class libraries, and Java class loader.