Java For Non-Programmers

RaynDrop : Tutorials : Java For Non-Programmers

Beginning programmers often fall into the trap of randomly trying different special words and combinations of words they've been taught in attempts to solve the problems they've been given. Beginners' tutorials don't seem to help much by delving too quickly into the code, such as Sun's own set of spotty tutorials. This introduction instead illustrates the main concepts behind the way those special words are combined in Java.

Objects

You've already heard that Java is an object-oriented language. This is simply one approach to programming. The idea is to program by framing the problem in terms of interactions and manipulations of objects. So what's an object? Let's take a brief detour to tell a story.

There are three people in the room. Bob, Joe, and Jane. I want to find the average age of these people, whom I've never actually met. What's the first thing I do? I ask them for their ages. I can't really tell for sure how old they are, so I have to ask. Once I've obtained their ages, I can figure out the average.

Two important points come out in this story, the first about objects. If I want to know something about an object, I must ask it. I can't just reach inside Bob and take his age - that's not a physical possibility. Likewise, there are certain ways to deal with objects and certain ways not to, and this is one of the main advantages of Java. I can create a certain kind of object and, for your own good, restrict the ways in which you can interact with it. These interactions are called methods.

The other point is the distinction between 'people' and 'Bob.' Bob is a person; another way of saying this in Java is that Bob is an instance of a person. In general, I ask people their age, but if I want Bob's age, I have to ask him specifically. There is some given way to interact with a type of object which all instances of that type will follow, though the results of those interactions will differ from instance to instance.

Abstraction

All this stuff about objects is really an attempt to enforce a deeper concept called abstraction. No matter which language and which style of programming is in use, code that's going to be used by someone else will be far more useable if the original programmer understands and programs using abstraction; Java, by design, does as much as it can to steer programmers into using abstraction.

Abstraction is like most electronic devices - people want to use the device, but the vast majority of users don't want to know how the device works and probably wouldn't understand even if it were explained to them. A computer monitor displays images coming from the computer; good monitors allow their users to adjust the size of the picture, the brightness, the contrast, and other various settings. While a user knows that these settings exist, they also know the only way to change them must be through the little buttons on the front of the monitor.

Likewise, Java programmers create objects with characteristics, like age for a people type of object, which can only be obtained through asking, the method. Abstraction almost definitely introduces more code, and sometimes more code can be problematic - but the advantages of abstraction far outweigh the disadvantages. If I, as the original programmer of the people object, decide that I want to calculate age in separate ways, by days, years, and seconds, then I can add methods to my object while leaving the original method, say, getAge, completely intact so other programmers who used my people object will still be safe. Abstraction guards anyone using your code from having to change their code whenever you change yours, making code reuseable, and decreasing the amount of work (and headache!) around the world. And that's a Very Good Thing.

Caveat: the word abstract has a particular meaning in Java. It follows in the tradition of the concept, abstraction, but don't pay too much attention to it for now.

Using Objects

Most of the time, an object must be instantiated. If I know about a people class, I'll have to invoke the appropriate words in Java to have an instance of a person. An example of code is the best way to illustrate, so I'll violate my own wariness about showing too much code to give an example here:

  objecttype name = new objecttype();

The objecttype is the kind of object I want to create, and the name is the way I'll refer to the object when I want to use it again. For an instance of a Person class I want to call John, the code would look like:

  Person John = new Person();

Like all hard and fast rules, there are exceptions. When I've created a class, I may want to provide methods that can be used without having any particular instance of it. These methods are poorly labelled static due to history.

Hierarchy(, scope?)

Security (public, private)

Non-objects (primitives)

Language and Grammar

As mentioned briefly in the very first paragraph, tutorials often fall into the trap of jumping into code examples far too quickly. These are primarily concerned with the way the language is actually written, its syntax. This is much like grammar in spoken languages. For instance, an English sentence should really have a noun, verb, and direct object in that order to make sense for other English speakers.

Java interpreters are already very good at determining when a programmer has mis-spoken in their program. Syntax errors cannot be bypassed; programmers new to the language will eventually learn through experience what the right thing is in terms of syntax through trial and error, but the concepts are usually what really bites someone.

Simple syntax explanation, common mistakes

Using the Java Tools

javac, java, classpath

Illuminating Example

Hm. Pen and paper? Or people (simplified)?

Wrap Up

Conceptual review, maybe like a quick-ref glossary