What's an object? The answer is simple, really. Objects in Java are very similar to the things we think of as objects: chairs, tables, cars, people, students, files, desks, drawers, money, bank accounts, and so on. Many people argue that object-oriented programming is more difficult for the beginning programmer to master, because students must understand the supposedly difficult concepts of the "object." However, we take the opposite view. Thinking about objects in computer programs is as natural as thinking about objects in the real world. It is something that we as individuals have done since a very young age. Furthermore, thinking about the world as consisting of objects is deeply rooted in the Western cultural tradition. Many of the ideas first explored by Plato and Aristotle over two thousand years ago have manifested themselves today in the technology of object-oriented programming.
Objects often (usually) are made up of other objects. In fact, very few objects are primitive -- most objects consist of other objects. Humans have organs; organs have tissues; tissues have cells; cells have DNA, proteins, mitochondria, and so on. Offices have desks; desks have drawers; drawers can have contents inside of them. Composition of objects is a central theme that we'll revisit throughout this study.
Let's take a detour and enrich our understanding of language by looking at how we often use it to describe the world around us. While this discussion may seem as if it couldn't be further from learning how to program a computer, it turns out to provide the fundamental basis for nearly everything we'll talk about for the rest of our study. Why? Because building an object-oriented computer program is all about using a simple language (such as Java, which is much simpler and less powerful than our own, natural language) to define a system of things and their relationships. We shall see that it is not at all radically different from the way we use language every day.
It is true that there is a difference between concepts and objects -- concepts are special sorts of objects that have descriptive and generative powers. They describe a set of objects that all share a certain makeup and qualities. They can also be used to generate new objects. It's easiest to see this with respect to Numbers. Understanding the concept of Number allows us, for instance, to generate all kinds of numbers, like 7, 42, or 3.1415. These differences do not mean that concepts themselves are not objects.
Java is a language which is far less descriptive, yet much more formal than our own language. That is, because we use Java to communicate with a machine, we must be totally explicit in what we say, and we must follow certain rules for how we say things. Java allows us, above all, to define Concepts, create Objects that are instances of those Concepts, and manipulate those Objects.
The terminology used in Java differs slightly from that which we have introduced above. The term object is identical; it is a thing. In Java, we use the term class instead of the word concept though. It's easy to remember this word if you remember that a concept describes a class (or set) of things. Our concept for "car" describes a set of things that are all cars. We will also use the word type as a synonym for class. There is actually a technical difference between the two terms, but for now we'll treat them as the same. Finally, just as we think of many real-world objects as being able to perform activities, Java objects can be "active" as well. In Java, if an object knows how to perform a certain activity (for instance, to drive a car) we say that it has a method. We can ask an object to perform an activity for us by sending it a message.
Note that in Java, all objects can have methods, that is, they may know how to perform certain actions for us. You should think of every object in Java as being "animated" or "alive". This ability to animate objects is what gives Java a special power: we can use it to define classes of objects, create objects of those classes, and finally ask those objects to do things for us. In Java everything is make-believe. We may animate everything. In animating all of our objects, we give them life. Part of being a good Java programmer is to imagine a world in which all objects are "smart", in which all objects can perform tasks on our behalf. Leaving behind the constraints of real-world limitations, and entering this make-believe world of animated objects allows us to create powerful systems of intercommunicating, interacting objects.
We will now proceed to re-introduce the fundamental concepts we've discussed above, but in the context of the Java language. At each step, it's important to think about the ideas we've discussed above. Think about how you consider objects, concepts, names, and so on in the real world, using real language. If you can do this, you will win most of the battle of becomming a good programmer. To be a good programmer is to know what you want to say. Once you know that, figuring out how to say it is almost mechanical. Many beginning programmers become obsessed or overwhelmed with how to say certain things in Java, when they haven't really even figured out what they want to be saying. You are already good at seeing the world in terms of objects, names, activities, and concepts. Leverage that skill as you step into a new way of expressing your ideas.
The meaning of a word is the thing that it refers to. It's OK to have two names that mean the same thing. For instance, I might also call my cat "Beast" sometimes, which would result in this picture:
Notice that in this particular context, we have two names, "Henry" and
"Beast" that both refer to the same object, my cat.
Notice that both the names indicate the same object, which is a quantity. Pretty much all of your intuitions and understanding of numbers apply equally in the Java world. In Java we may express numbers literally and give interesting names to them:
int score = 59; double todaysTemperature = 67.4;We'll use two of the different kinds of numbers provided by the Java language throughout this text: we'll use an
int
when we
want to represent integral values; we'll use a double
to
represent rational values.
Throughout this study, we'll draw pictures to help understand and
express the relationships between the things in our programs. Let's
draw one:
The above picture shows the name "score" as referencing or denoting (with the arrow) a thing, which in this case is a quantity. Above, we represented a quantity as a cloud (or a blob), to differentiate it from its name (in that example, "3" or "III"). We could have used a blob in this picture as well, but they're not terribly descriptive. So we'll just represent quantities by their common names -- in this case, 59. The way to read a picture like the above is: "The name 'score' refers to the quantity (or number) 59."
The above example also introduces us to our first pattern, which is a pattern for naming things. Sometimes, we'll also call this binding a name to a value.
<The kind of thing> <the name> = <the thing we're naming>;
Notice that Java is sensitive to case when we write names. In other
words, the name score
and Score
are not the
same name in Java. Furthermore, Java imposes some restrictions on the
legal letters in our names, which we also call identifiers
sometimes. The rule is essentially that names must begin with either
a '_' or a lower or upper case 'a' through 'z' character. The rest of
the letters can consist of lower and upper case 'a' through 'z', '0'
through '9' and '_'.
Another useful primitive object is the char
, which is
used to represent individual characters, such as 'b'. Here are a few
examples of characters in use:
char letterGrade = 'A'; char middleInitial = 'R'; char section = 'b';Finally, Java also provides us with a kind of thing called
boolean
, which we can use to talk about the truth or
falsehood, for instance:
boolean rainingInSeattle = true; boolean rainingInLosAngeles = false;A boolean can have just one of two values, true or false. We can name these values directly in Java, just like we can name integer values directly.
Circle theMoon = new Circle(100, 200, 25); Rectangle aBox = new Rectangle(30, 40, 10, 20);Note the special word "new" used above. This is the way we ask the machine to create a new instance of an object for us. Why don't we need to say
new
when we use numbers? In a way, we never
need to create new numbers: imagine that the Java world is inhabited
by numbers in much the same way as our own and we may just name them
and use them at will.
Lets draw some pictures of our new objects:
theMoon +------> x +------> 100 y +------> 200 radius +------> 25 aBox +------> x +------> 30 y +------> 40 width +------> 10 height +------> 20
Let's look at naming in a little more detail. We say that a name refers to an object. For this reason, we can have multiple names that refer to the same object. For instance, the names "Mom" and "Ellen" may both refer to the same person, in this case, my mother. Let's look at a Java example:
Rectangle aBox = new Rectangle(30, 40, 10, 20); Rectangle theSameBox = aBox;Here's the resulting picture we might draw:
Finally, we can also send some messages to these objects:
int area = theMoon.area(); aBox.move(10, 15);The first statement is asking the moon to compute its area, and giving the returned value a name. The second statement is asking the box to move a certain distance in the X and Y directions. We will often speak of objects as having methods. A method is a message that the object understands. For instance, we might say that a rectangle object has the
move
method. The basic pattern
for sending a message to an object is the following:
<object-name>.<message-name>(<some information>);We sometimes call the object that is receiving the message the receiver. It's useful to step back into real language for a moment to get a handle on this particular bit of syntax. Look at the following English sentence:
Bob eats pizza.At some point, our language teachers totured us with the exercise of diagramming sentences, that is, breaking them down into their constituent parts. Let's do that to the above sentence:
Bob [subject] eats [verb] pizza [object]Pretty easy. This pattern is as fundamental to sentences in the languages that humans speak as it is fundamental to Java. In Java, the receiver object is just the "subject" of the sentence. The message we send to the object is the verb or verb clause. In fact, you'll notice that method names are almost always verb clauses. Finally, the information that we send along with the message is the "object" of the sentence. Java just happens to use a different syntax (a different way of writing) to express these kinds of sentences. We could imagine rewriting the above sentence in a more Java-oriented way:
Bob.eat(pizza);
String
.
String name = "Billy";We surround the literal representation of the string in double quotes to distinguish it as an object we wish to use. Note that we use double quotes to write Strings but single quotes for characters. You can also send Strings messages, for instance:
int lengthOfName = name.length(); String upperCaseName = name.toUpperCase(); String newName = name.replace('B', 'W');
ArrayList
. Here's an example:
ArrayList classList = new ArrayList(); classList.add("Bob"); classList.add("Ellen"); classList.add("Johanna");The above collection now contains three objects, which are all Strings, used to represent the name of three students. How do we figure out who has the longest name? We don't know how to do this in Java yet, and won't for a few more lessons, but you already know how to do this. Given a real class list, think about how you might give a well-defined set of instructions, or algorithm, for finding the longest name.
bold
):
prompt> String name = "John"; prompt> name "John" prompt> String upperName = name.toUpperCase(); prompt> upperName "JOHN" prompt> name.length() + 10 14