Developing a Java Application

Writing a Java application consists of several steps: writing the code, compiling the code, and executing the application. Java source code is stored in a text file with the extension .java. Compiling the code creates one or more .class files, which contain processor-independent byte codes.

Screen Shot 2018-12-07 at 5.29.57 PMThe Java Virtual Machine (JVM) then translates the byte codes into machine-level instructions for the processor on which the Java application is running.

Thus, if a Java application is running on an Intel Core i9-9900K processor, the JVM translates the byte codes into the i9-9900K’s instruction set.

You can get a Java SE Development Toolkit (JDK) on the Oracle website free of charge. The JDK allows you to build applications, applets, and components using the Java programming language. In addition, the JDK contains a broad range of prewritten Java classes that programmers can use in their Java applications. If you are downloading and installing Java yourself, be sure to choose your appropriate operating system when you download, and follow the directions on the Oracle website, including the directions for setting the path for javac, the Java compiler. You need to set the path correctly so that you can run the Java compiler from any directory on your computer. To develop an application using the JDK, write the source code using any text editor, such as Notepad on Windows, TextEdit on Macs, or the vi editor on a Unix operating system. To compile the code, invoke the compiler from the command line:

Screen Shot 2018-12-07 at 5.13.43 PM

where ClassName.java is the name of the source file. So remember that when you save the source code you write into the text editor, this must end in .java.

If your program, written in the file ClassName.java, compiles correctly, a new file, ClassName.class, will be created in your current directory. To run the application, you invoke the JVM from the command line:

Screen Shot 2018-12-07 at 5.40.21 PM

Okay, now never do that again.

Typically, programmers use an Integrated Development Environment (IDE) instead of a text editor and the command line to develop applications. An IDE consists of a program editor, a compiler, and a run-time environment, integrated via a Graphical User Interface. One of the many advantages to using an IDE is that errors in the Java code that are found by the compiler or the JVM can be linked directly to the program editor at the line in the source file that caused the error. Everything looks nicer too – the Graphical User Interface enables you to have the editor and compiler in the same place, and therefore execute the program without launching separate applications:

Screen Shot 2018-12-07 at 5.56.33 PM

 

Some of the many available IDEs include Eclipse ; intelliJ IDEA; BlueJ. Use Google for more.

Pick one at random.

Short Intro To Object-Oriented Programming

Let’s recall what high-level languages are: these are languages that are symbolic, so not the ones and zeros underneath it all. They are that which provides handles for the human mind to pilot the Eva – in other words complete the task of serving a functional role in a complex society that you as an individual did not create. The way you do this is by learning symbols and how to manipulate these.

Like with all good creation myths, in the beginning, there were two high-level languages, Fortran and Pascal. These were procedural. What that means is that the program starts with a problem and then breaks that problem down into smaller sub-problems or sub-procedures.

Procedural language took a literal approach, working as a straightforward story composed of top-down instructions.

But a program inevitably has sections. Typically, programmers of old had to write task-specific code in separate procedures/functions, and invoked these procedures from one section of the program to another in order to perform various tasks. And the program’s data was generally shared among the procedures – there were no membranes enclosing different cells in the original primordial soup.

In the mid-1970s, the first object-oriented programming language, Smalltalk, was born from the sins of the procedural languages, enabling programmers to write code with a different approach. Whereas procedural languages were brutish in that they did not recycle code by attempting to enclose into concepts and dealt mainly with basic data types such as integers, real numbers, or single characters, Smalltalk provided the programmer with a new tool: classes and objects of those classes. Classes are simply concepts. Like ramen is a class containing the objects of noodles, broth, egg, bowl. If you had to tell the chef to place noodles, broth, and egg, into a bowl every time you walked into a ramen-ya instead of just telling them “give me ramen,” that would be annoying.

The class enables the programmer to encapsulate data, creating membranes. This is how spoken language itself works. A tribe in northern Namibia packages particular wavelengths of light this way:

Screen Shot 2018-12-06 at 5.35.29 PM

Take a look at buru. For you, buru does not look like a single color. You speak a language that does not put a single membrane on those wavelengths. You instead have a class for each. Perhaps you call them green and blue. Once you see it one way it is short of impossible to see it the other. This is the same with language processing – their tribal language is gibberish, but these words on the screen are helplessly understood. That’s the power of classes.

A class defines a template/model, from which objects are created. Creating an object is called instantiation. Thus, objects are created/instantiated according to the design of the class. A class could represent something we imagine to be real such as a person. The class could have various attributes such as in the example of a “person” class: a name, an attractiveness rating, and an age. The class also provides code called methods. Methods allow the creator of the object to set and retrieve the values of the attributes – granting the power to christen the name as Lindsey, the rating as a solid ten, and to then to reveal this name and attractiveness rating when needed.

One big advantage to object-oriented programming is that well-written classes can be reused by new programs, thereby reducing future development time. (This is just in the same way that We reduce your development time by providing you with over five thousand five hundred revolutions around the sun head-start on the human soul packaged in language. This apex is then accessed through the internet and allows sufficiently maniacally self-motivated teens to ascend to heights of knowledge that would otherwise not be accessible even to those with genius-level IQ.)

In this noble endeavor of conserving negentropy through reusability, Smalltalk was somewhat successful, but had a major fault: its syntax was unlike any syntax already known by most programmers. Most programmers who knew C found themselves in a dilemma. They were attracted by the object-oriented features of Smalltalk but were reluctant to use it because its syntax was so different from C’s syntax. Humans are notoriously allergic to changes in syntax, even when a change in syntax might result in easier understanding of certain concepts.

C++ added object-oriented features to C, but also added complexity. Meanwhile, the Internet was swallowing mankind by the day. Web developers used HTML to develop webpages and rapidly had to incorporate programming features not only on the server side, but also directly on the client side. Fortunately, Java emanated from the void.

Java Inheritance Part 2

The syntax for defining a subclass class that inherits from another class is to add an extends clause in the class header:

Screen Shot 2018-06-30 at 1.54.33 PM

The extends keyword specifies that the subclass inherits members of the superclass. That means that the subclass begins with a set of predefined methods and fields inherited from its hierarchy of superclasses.

JFrame allows us to create graphical applications. So we can use that to create our subclass of StatisticalDispersion and all of its inheriting subclasses.

Here, StatisticalDispersion is the subclass under JFrame. And it inherits from all the classes that JFrame inherits from, all the way back to the Object class.

Screen Shot 2018-06-30 at 3.24.57 PM

The StatisticalDispersion Class Hierarchy

We are coding a class named StatisticalDispersion that extends the JFrame class, so we use the following header:

Screen Shot 2018-06-30 at 3.44.19 PM

Because our StatisticalDispersion class extends JFrame, it inherits more than 300 methods and more than 30 fields. That’s because the JFrame class is a subclass of Frame, which is a subclass of Window, which is a subclass of Container, which is a subclass of Component, which is a subclass of Object.

A hierarchy is composed of subclasses which inherit methods and fields. Here, we make all of them available to the StatisticalDispersion class.

Subclasses do not necessarily need to use their inherited methods, but these are available if needed. The programmer does not need to write methods and define fields in classes which have already inherited them.

JFrame is the direct superclass of our StatisticalDispersion class. As you can see in the image, StatisticalDispersion refers to it. And as you can see in the code, JFrame follows the extends clause.

A class can have multiple direct subclasses but only one direct superclass. One can have many offspring but yet can only develop from one zygote.

Inheritance In Java

Sometimes in programming we want to reuse a class. One of the most common ways to reuse a class is through something called inheritance.

will2

A program can contain many classes. Some of these classes can be organized into ordered levels of functionality. When we have an ordered level of functionality, this is called a hierarchy. In fact, you are in such a hierarchical program at this moment:

Da4CTWDVwAAne0M

So say we want to create a hierarchy of our own. How do we do it?

We begin by defining a class that contains methods and fields that are common to all classes in the hierarchy. Methods are the verbs and fields are the nouns. Fields come in two flavors: instance variables and class variables. Java Concept Of The Day has a great explanation of the difference between these two kinds of variables.

Then we define new classes at the next level of the hierarchy, which inherit the behavior and fields of the original class. Some books call these inheriting levels ‘lower levels’ but that is just… well… idiotic. What should be called lower level are the quarks, and the stars that inherit their behavior are higher level in the hierarchy.

Yes. I am openly defying whoever in computer science decided to invert what we call up and down. I think the guy should die and his followers too. In every other science the emergent properties are called higher, not lower.

Okay, back to the task.

In the new classes, we define additional fields and more specific methods.

One will not find writ in the Standard Model of Particle Physics a single mention of late 20th-Century German Socialism. And yet, the motions and thoughts of Günter Herburger and all the other highly specific actors of the human stage inherit the burden of obeying the Standard Model. We are made of its fields and play by its methods. …As in nature, so in Java.

The original class is called the superclass, and the new classes that inherit from the superclass are called subclasses. Biology inherits from chemistry and chemistry inherits from physics. Physics would be the superclass in an academic department. That is, until the mathematician gets word.

Some object oriented programming developers call a superclass the base class and call a subclass the derived class. As in science, a superclass can have multiple subclasses, and each subclass can be a superclass of other subclasses and so on. Physics has thermodynamics and biophysics, and these in turn spawn more specialized sub-disciplines. Thus, a class can be both a subclass and a superclass. In contrast to science, however, Java subclasses inherit directly from only one superclass.

A subclass can add fields and methods, some of which may override, or hide, a field or method inherited from a superclass. In real life, the best example of this is the Born Rule of quantum mechanics. Our observed reality is a subclass of the universal wavefunction which, by the Born Probabilities, masks the rest of the program (i.e. infinite madness).

So let’s look at an example. One of the most important things in the world (at least for those of us who want to be effective in the world) is to understand statistics. So lets use a statistics example.

An important concept in both the truth-seeking enterprise of science and the money-seeking enterprise of capitalism is statistical dispersion. There are several kinds of statistical dispersions used to extract insights from data. One kind of statistical dispersion is variance, and another is standard deviation. Standard deviation can be further subdivided into corrected-sample standard deviation and uncorrected-sample standard deviation.

So lets turn the statistical dispersion types into classes and construct a hierarchy. We define a StatisticalDispersion class as a superclass. We then define a Variance class that inherits from StatisticalDispersion. We also define a StandardDeviation class, which also inherits from StatisticalDispersion. We further refine our classes by defining an UncorrectedSampleStandardDeviation class and a CorrectedSampleStandardDeviation class, both of which inherit from the StandardDeviation class. The figure below depicts a subclass and superclass relation.

Screen Shot 2018-06-29 at 8.56.14 PM

Arrows pointing from a subclass to a superclass indicate that the subclass refers to the superclass for some of its methods and fields. What is at bottom is StatisticalDispersion. It is from this class that all the other classes inherit code. Don’t allow the direction of the arrows to confuse you.

The entire hierarchy can be represented using a UML (Unified Modeling Language) diagram. The boxes below the class name are available for specifying instance variables and methods for each class. For simplicity, I will leave those boxes blank. Later, I will illustrate UML diagrams complete with fields and methods.

Screen Shot 2018-06-29 at 9.55.27 PM

StatisticalDispersion Class Hierarchy

The Java Class Library contains many class hierarchies. At the root of all Java class hierarchies is the Object class, the superclass for all classes. Thus, all classes inherit from the Object class. The most important advantage to inheritance is that in a hierarchy of classes, we write the common code only once. After the common code has been tested, we can reuse it with confidence by inheriting it into the subclasses. And when that common code needs revision, we need to revise the code in only one place.