A Few Cups of Java: Thinking Like a Programmer

Programming is rapidly becoming a skill matching writing and math in its applicability, and as the world becomes more and more computerized, knowing how to write code becomes more and more important.

Today we embark on a journey to learn programming, particularly in the Java language. Java is a programming language that is easy to learn, and transitions well to many other languages. It is also an 'object-oriented' language (more on that later). It's totally free, has lots of support on the internet, and comes with good documentation.

Before we jump into Java, we need to look at a few concepts that help you to THINK like a Programmer.

Java is an Object Oriented language.

In the old days, programs were literally just sets of instructions a computer read from start to finish. Programs were procedures for a computer to follow.

If you needed to use a textbox, for example, in your program to allow the user to input data, you would have to code it from scratch each time you want to use it. In object oriented programming, we have what are called classes, which are sort of like blueprints for how to make something. In Java, to make a textbox, we just make an INSTANCE of the TextBox class.

An object is an instance of a class.

Similar to how a blueprint is used to make buildings, classes are the blueprints we use to make objects. Classes contain properties and functions. Properties describe things about an object (its height, width, color, etc) while functions define what an object can do (roll, walk, sit, stand, etc).

Let's think in terms of buildings. You're tasked with making two buildings, both of which are almost identical in terms of structure, but are different in

  • The number of stories high they are
  • The number of windows they have

Rather than making two totally different blueprints for almost the same thing, we could make one blueprint and build two buildings with it, only modifying the two properties (height and number of windows).

To do this we would make a class, called Building. This Building class would define what a building IS, and allow us to make buildings by defining their characteristics. In Java, a VERY basic shell of such a class would look something like:

class Building{
    int height;
    int windows;

The curly brackets ( { } ) are used to denote groups of lines of code. Each line of code will end in a semicolon (;), remember to put these there. We don't put semicolons at the ends of groups, though, only lines.

If we need to make two buildings, one called shortBuilding, and another called tall Building, we could do it like this, in Java

Building shortBuilding = new Building();
Building tallBuilding = new Building();

The above is how you make objects in Java. An object is like the building you would make using the blueprint. Of course, the building isn't the blueprint itself, but it is made from the blueprint. The general structure for instantiating objects (making objects from classes) in Java is:

    Type name = new Type();

Where we would replace Type with the name of the class, and replace name with whatever we want to name that object.

Classes can have Sub-Classes, which inherit all the functions and properties of the parent class

We now know that classes are like blueprints, a set of definitions used to make things. Let's look at balls as an example.

What IS a ball? Well, balls are round, so they can roll (function), and they are generally spheres, so they have a radius (property).

Imagine if rather than talking about the class of objects called balls, we refered to them by their definition.

"My little brother was playing with his round spherical object which bounces."

"The boy kicked the round spherical object which was black and white."

Doing so is incredibly impractical, Instead, we can think of round/spherical things being a class of objects, we'll call the class Ball.

But then we'd still talk about soccer balls as black-and-white Balls, and tennis balls as green bouncy balls which are covered in felt.

Rather than doing this, we define the properties of a ball in a class, and make sub-classes, or child classes, called SoccerBall and TennisBall. When someone says tennis ball, it's obvious they's talking about a round, spherical object, because the TennisBall class is a child of the Ball class. It can roll, and it's round, but it also has special properties of its own. This way we see that child classes inherit the functions and properties of their parent classes.

These ideas (classes, objects, inheritence) lie at the core of object-oriented programming. Becoming used to thinking in terms of objects, classes, and properties will aid you while learning Java, and will make the whole experience more intuitive.

Next time, we'll be learning to store data in variables!

Here's to your quest to learn programming!