Objects


Objects

Java is based on the idea of an object. Objects have two key characteristics.

  1. An object holds some information. For example, a String object knows a sequence of characters. An object that represents an image, such as a photograph, stores all of the information in the image.

  2. An object can do jobs for you. It has a limited set of capabilities, most of which involve either telling you something about the information that the object is remembering or making some changes to that information.


Dot notation

If x is an object and request is a request that x understands, then you write x.request to ask x to respond to the request. Notice the dot between the object and the request.

We have seen expressions and statements. An expression computes a result, and a statement just performs an action.

Some requests to objects are expressions. For example, a string object can tell you its length. If s is a string object then s.length() is an expression whose value is the length of string s. For example, you can write

  int len = s.length();

Some requests to objects are statements. You just ask the object to do something. For example, there is an object whose name is System.out. (The name has two parts, separated by a dot. The dot has more than one purpose.) You can ask object System.out to display a string on the console. Java statement

  System.out.println("Hello").
has the same effect as Cinnameg statement
  Displayln "Hello".
and Java statement
  System.out.print("Hello").
has the same effect as Cinnameg statement
  Display "Hello".
In Java, to get a job done, you need to find (or often create) an object that knows how to do that job, and explicitly ask that object to do it. An action has an agent who performs it.


Terminology

In object-oriented terminology, the capabilities that an object possesses are called methods. For example, a String object can be asked to perform the length method, and object System.out can perform a method called println. A method can have arguments that provide specific information. For example, the println method has an argument that is the string to display.

Object references and object equality

Return for the moment to the viewpoint that an object is a particular host on the internet. You can refer to a host via a URL, such as http://www.yahoo.com, and two people can refer to the same host by using the same URL. The important (and obvious) thing to notice is that there is not a separate host for each person. They both refer to the same host.

Programs refer to real objects not by URLs but by numbers. For example, there is object number 1, object number 2, etc. When you create an object, a new number is chosen for it. A variable that you think of as containing an object actually contains an object number that refers to the object.

Using object numbers, called object references, is important when you change the information in an object. Suppose that variable x refers to object number 12. When you do statement

  y = x;
you store the same object reference, 12, into variable y. Now if you tell x to change its information, you are telling object number 12 to change itself. Since y refers to the same object (number 12), you will see that object y has also changed its information. Variables x and y refer to the same object.

Using references can be summarized by saying that objects are not automatically copied.

(It is possible to make a copy of an object. Expression x.clone( ) yields a copy of the object to which x refers, and has a different object number. But you must explicitly ask for a clone if you want one.)

You can ask whether two variables refer to the same object. Expression x == y is true if x and y refer to the same object. It does not ask whether x and y hold the same information, but whether they refer to exactly the same object.


Problems

  1. [solve] What are the characteristics of an object, in general?

  2. [solve] A string object can respond to the request charAt(n). That request has an answer that is the (n+1)st character in the string. If s is a string object, how would you set variable c, of type char, to the first character of s? What notation do you use?

  3. [solve] Suppose that x and y are two variables that refer to objects. When is expression x == y true?


Summary

An object remembers some information and can process requests either to get or to modify the information.

Write a dot after an object, followed by a request, to ask the object to process the request. For requests that have answers, you get an expression. For requests without answers, you get a statement.

Objects are not copied unless you make an explicit request to copy (or clone them). Expression x == y is true if variables x and y refer to the same object.