Java - The Guts


What are objects?

Java is an "object-oriented" programing language.
Are objects a great new way to program computers or just a way to hide what the computer is really doing? I vote for both, but more for the latter. The truth is that computers, even "virtual machine" computers, still pretty much do one operation at a time but do it so fast that it looks to humans like they are doing a lot of stuff at the same time. You can add multiprocessing and multithreading, but it is still the same old stuff we have done on the mainframes for 20 years. Now the little computers are fast enough to do it and the applications and user demands are complex enough to need it. The object oriented programming languages, especially Java, which almost forces that style, are really just a way to encourage modular and structured programming techniques developed over the years on mainframe projects. To the computer, code is just another flavor of data that can be crunched in another way. Objects bundle code and data into relatively indivisible units, but under the covers things are still just step-by-step procedural code - it is just a lot harder to figure out what order the code is going to be executed in. This course will attempt to demystify the deep dark secrets right up front on what executes when and why.

In a mainframe sense, an object is like a "started task" with program code which has been loaded into memory and has been called at it's initialization exit to set up it's current execution environment. The difference is that it may or may not have asyncronous execution - it may be designed to execute only as a subroutine of other "tasks". Independent execution power for Java objects is called "threads".

Java programs look something like this:
 
 
Define a "class"
Define some data items aka variables
Define some "methods" aka functions or subroutines

What are classes?

Are classes really "object factories" ("cookie cutters" for objects) or just a way to obscure the data/code connection? Actually, a Java application or applet is a group of related program modules called classes. Practically speaking, to develop a Java program, you write a class definition and save it in a "xyz.java" file, then compile it to create an "xyz.class" file which contains the Java byte code for the program module. When an application runs, it can "instantiate" a class (code+data) perhaps multiple times which creates multiple "objects". My guess is that they are called classes because you may have multiple objects of that class which have been created with the same class module.

What are data types?

When programming in Java, classes are also referred to as "types" because a data item can be a "primitive" type such as an integer, floating point number or string, or it could be a reference to a complex "class" type of object. For example, if you wrote a class and called it "Clown", you could define a variable in a Java program called "bozo" by saying this: "Clown bozo;". The variable "bozo" is then said to be of type Clown and can only be used as a reference to a Clown object.

To make things even more confusing, a Java class can be loaded and executed without creating an object from that class. This is exactly what happens when you first start a Java application with the Java Virtual Machine (JVM). The JVM loads the class file you specify and calls the "main" entry point in the class file without creating an "object" of that class. This means that only code and data that is defined as "static", in other words belonging to the class and not objects of the class, can be accessed, since no object of that class has been created. This sort of makes sense from the standpoint that the JVM does not know what you want to do initially, so it lets you decide if you need your code to be an object, or if you want to create some other objects from other code. This can lead to some problems, if you don't know what is happening to your code, which we will see later. Where this behaviour actually makes sense is in the case of a subroutine package, called an "abstract class", such as the "Math" class which contains functions like "sqrt". There is no "Math" object, but it is handy to be able to pass other objects to the math functions.

What are Methods?

Note that Java calls subroutines or functions "methods". Java methods may or may not accept parameters and return values. Most Java code is contained in methods. Java classes consist of data items and methods. The "main" entry point for a Java application is a method defined like this:

"public static main(String argv[])"
The "public" property means the method can be called from other programs, in this case probably the JVM itself. The "static" part means the code is shared by and exists independently from objects of the class type. (Ignore the parameter "String argv[]" for now - it is just an array of the parms you specified on your command line to start the Java application.)

How do you create an object then?

To actually create an object in Java, you have to "instantiate" it. This not only loads the code and initializes data items, but may run the object's code. Objects are created by using the special Java keyword "new". To create a "Clown" object you would use this statement:

bozo = new Clown( );
This loads the Clown class code and variables and calls the initialization code in the Clown class. The initialization code is a method called a "constructor" because it is supposed to "construct" the object, I guess. We will talk more about that later.

Subclasses

When you define a Java class, you can specify that it is actually an extension or add-on to another Java class. This is a very common way to define classes in Java, because it gives your class object access to the data and methods of the class you extend. This keeps you from having to reinvent everything from ground zero when you write a new program. In fact, if you look at methods in Java programs, you will see that, aside from the usual control constructs and assignments, most of the code is calls to methods in other objects. That is how everything is done in Java. Very little is done directly. In tech talk, when you "extend" a class you "inherit" it's "properties" (data items and methods) which makes you it's "child" or "subclass" and it your "parent" class. In the Java language this looks like this:

class MyApplet extends Applet;