What Are Objects in Java Programming? - dummies

What Are Objects in Java Programming?

By Doug Lowe

Object-oriented programming is a type of computer programming — such as Java — based on the premise that all programs are essentially computer-based simulations of real-world objects or abstract concepts. For example:

  • Flight-simulator programs attempt to mimic the behavior of real airplanes.

  • Many computer games are simulations of actual games that humans play, such as baseball, NASCAR racing, and chess.

  • Even business programs can be thought of as simulations of business processes, such as order taking, customer service, shipping, and billing.

Objects are programming entities that have certain basic characteristics:

  • Identity: Every object in an object-oriented program has an identity. In other words, every occurrence of a particular type of object —an instance can be distinguished from every other occurrence of the same type of object as well as from objects of other types.

    Each object instance has its own location in the computer’s memory. Thus, two objects, even though they may be of the same type, have their own distinct memory locations. The address of the starting location for an object provides a way of distinguishing one object from another because no two objects can occupy the same location in memory.

    Java keeps each object’s identity pretty much to itself. In other words, there’s no easy way to get the memory address of an object; Java figures that it’s none of your business, and rightfully so. If Java made that information readily available to you, you’d be tempted to tinker with it, which could cause all sorts of problems, as any C or C++ programmer can tell you.

    Java objects have something called a hash code, which is an int value that’s automatically generated for every object and almost represents the object’s identity. In most cases, the hash code for an object is based on the object’s memory address, but not always. Java doesn’t guarantee that two distinct objects won’t have the same hash code.

    When used with objects, the equality operator (==) actually tests the object identity of two variables or expressions. If they refer to the same object instance, the two variables or expressions are considered equal.

  • Type: Object-oriented programming lets you assign names to the different kind of objects in a program. In Java, classes define types. Therefore, when you create an object from a type, you’re saying that the object is of the type specified by the class. The following example statement creates an object of type Invoice:

    Invoice i = new Invoice();

    In this case, the identity of this object (that is, its address in memory) is assigned to the variable i, which the compiler knows can hold references to objects of type Invoice.

  • State: Although each instance of a given object type has the same attributes, each instance has a different state: that is, a different combination of values for each of its attributes.

    Although some attributes of an object are public, others can be private. The private attributes may be vital to the internal operation of the object, but no one outside the object knows that they exist. They’re like your private thoughts: They affect what you say and do, but nobody knows them but you.

  • Behavior: Another characteristic of objects is that they have behavior, which means that they can do things. Like state, the specific behavior of an object depends on its type. Unlike state, though, behavior isn’t different for each instance of a type.

    Suppose that all the students in a classroom have calculators of the same type. Ask them all to pull out the calculators and add any two numbers. All the calculators display a different number, but they all add in the same way; that is, they all have a different state but the same behavior.

    Another way to say that objects have behavior is to say that they provide services that can be used by other objects. You’ve likely already seen plenty of examples of objects that provide services to other objects. Objects created from the NumberFormat class, for example, provide formatting services that turn numeric values into nicely formatted strings, such as $32.95.

    In Java, the behavior of an object is provided by its methods. Thus, the format method of the NumberFormat class is what provides the formatting behavior for NumberFormat objects.