The Programming Language Java
In this lesson, we will introduce you to Java, and its advantages.
Advantages
Created by James Gosling of Sun Microsystems, Java was presented to the public in 1995. In 2010, Oracle Corporation acquired Sun Microsystems. Compared to other programming languages, Java isn’t as old. Although it was originally created for the computers in home appliances, Java has become an important general-purpose language with a widespread appeal. Java programs have an advantage because they:
- Are object-oriented
- Are platform-independent, meaning they can run on all kinds of computers
- Can run locally and remotely
Let’s talk briefly about each one of these points.
Object-oriented programming
Within an object-oriented program (OOP), we define basic elements called objects by writing Java code. An object stores certain values, or attributes, that give it a particular state. An object also has behaviors, some of which could change its state.
Java objects represent actual things
For example, Java objects often represent actual things, such as people, books, or cell phones. We can see that each of these real-life objects has characteristics or attributes, and some have behaviors. While not all things have behaviors, the Java objects that represent them almost certainly will. For instance, an object representing a rock should be able to give us its weight. The weight is an attribute of the object, and the act of providing the weight is a behavior.
Java objects represent abstractions
Java objects also can represent abstractions, such as names, songs, numbers, or bank accounts. While these items have attributes in real life, their behaviors likely exist only in the realm of a Java program. For example, a song object should be able to give you its title and composer.
Classes
An object has data fields to represent its attributes as well as methods to perform its behaviors. These data fields and methods are defined within a class that describes like objects. A class is like a blueprint or a plan for creating objects. We can write our own classes and thereby create objects of our own design, and can use classes that others have written. Java comes with a collection of useful classes, called the Java Class Library. This chapter and the Debugging Interlude chapter will show us how to use some of the classes in this library. The chapter, Class Definitions, will begin to show us how to write our own classes.
Frequently, a new class is based upon an existing class. By using a feature of object-oriented programming called inheritance, we can have a class inherit the fields and methods of another class, adding to or revising them as necessary. Inheritance provides a way to create new classes without repeating earlier work.
Platform independence
A platform is basically a kind of computer, that is, a computer’s architecture and often its operating system. For example, two popular platforms might be described as a Windows machine and a Macintosh running MacOS X. For many programming languages, a compiler must be written for each platform. The compiler for a given platform translates a program written in a particular language into the appropriate machine language for that platform. The disadvantage to this approach is the need for several compilers for the same language.
Java, on the other hand, is platform-independent. A Java compiler does not produce machine language instructions for a particular platform directly from a Java program. Instead, it generates an intermediate form of the program called the Java bytecode. This bytecode will run on any computer that has the Java Virtual Machine, or JVM, installed. The JVM is actually another program that converts Java bytecode into the machine language of its host platform. Any platform that has its own version of a Java Virtual Machine will be able to run a Java program.
Get hands-on with 1400+ tech skills courses.