As we have already mentioned in one of our other articles, "Where is Java used?", more than 3 billion devices in the world run on Java. So why is Java so popular? There are four main reasons:
1. "Write Once, Run Anywhere” (WORA)
The main advantage of the Java language is that the same code can run on different operating systems:
If you were to write using a different programming language, you would have to write 3 different codes: one for Windows, one for Linux, and one for MacOs. This feature (“write once, run anywhere”) makes Java cross-platform. You're probably wondering how Java manages this. The diagram below will explain.
- When you write code in Java, each program is saved as a separate file. Such files always bear the file extension .java. For example, Program.java (in the diagram above). So if your Java colleagues need to work with this file, it's for them to read, rewrite, or add something to the file. This is possible because the code is human-readable.
- In addition, when you run the programming language compiler (to be exact, it’s called the javac compiler), your program is converted from human-readable code to machine-readable bytecode (i.e. different combinations of 0 and 1). This creates another file with the file extension .class. In the example above, it's Program.class.
- Then the JVM (Java Virtual Machine) executes the Java bytecode.
2. User-Friendly Syntax
The creators of Java didn’t try to reinvent the wheel. Instead, they:
- took all the best parts from the the C programming language and it's descendant C++
- didn't take the parts of C and C++ that they considered unnecessary and not very successful
- and added innovative touches to the Java programming language
And this approach was very successful. Since there is a lot in common between C, C++, and Java, it was easy for programmers to switch from these languages to Java. They didn't have to learn everything from scratch, because many of the constructions of the Java syntax were familiar to them. This helps explain the consequent rapid growth in the popularity of Java among programmers.
3. Object-Oriented Language
Here, we will attempt to briefly explain what object-oriented programming (OOP) is. Later, in the Java OOP section, there will be an article "What is OOP?" with much more details. In the meantime, this explanation will be sufficient.
OOP is a programming paradigm based on the concept of "classes" and "objects." For starters, let's try to understand what an "object" is and then later we will move onto the concept of "class."
Basically, everything around us is an object. For example,
- a car is an object
- a human being is an object
- a cat is an object
- a dog is an object
- a table is an object
- and so on and so forth
Each object has its own characteristics. For example, the characteristics of a car are its model, color, size, etc.
Each object has methods ( i.e. actions) which it can perform. For example, the methods of a car are to slow down or speed up.
As we have already mentioned, OOP (object-oriented programming) is a programming paradigm based on the concept of classes and objects. So what is a class?
A class is the blueprint from which objects are created. Here are a few examples of classes:
Example No. 1
All cats are different:
- with short hair
- with long hair
- without hair, etc.
At the same time, cats have a lot in common. Well, then if we identify these common features as a separate Cat class, then we can use this class as a basis whenever we need to create a cat. Thus, the class works as a template for the creation of cats. And then we can specify additional features, i.e. the particular characteristics of each particular cat.
Example No. 2
All cars are different:
- Passenger cars
- Budget cars
- Luxury cars
However, cars have a lot in common. So if we identify these common features as a separate Car class, then we can use this class as a basis every time we need to create a car. In this example, the class would work as a template for the creation of cars. And then we can specify additional features, i.e. the particular characteristics of each particular car.
A kindly reminder: OOP (object-oriented programming) is programming paradigm based on the concept of classes and objects. However, we will talk about OOP in details in the Java OOP section.