Stack and Heap                                    
                                   
In the memory managed by the JVM behind the scenes, object references and the objects assigned to them are
managed by
stacks and the heap. Stacks are a list of references defined within a given block of code (also
known as a
scope). Recall from the last chapter that a code block is defined by { and }. The heap is where all
objects are actually stored in memory. References in different stacks can point to the same objects on the
heap.

The diagram below shows how the following code translates to memory.

     Customer c = new Customer();

















C and C++ developers should not confuse “references” with the C “pointer,” even though it is often said that
the reference is “pointing to” an object.

Using Objects           
                                                                    
Now that you know how to create objects, how do you use them? In the next set of examples, you will
explore a String object to see how to use objects. The String object represents an array of characters. Strings
are probably the most often used object.


String s =  new String(“Hello World   ”);


After you create an object, you can use its functionality to perform specific tasks.


String sub = s.substring(4,10); //returns "o Worl"
String trimmed = s.trim();      //returns "Hello World" (removeing extra
spaces)
String upper = s.toUpperCase(); //returns "HELLO WORLD   "
char c = s.charAt(2);           //returns 'l'


In the example above, procedures defined for String were used to help carry out different jobs.

Methods (Java's official term) are known by several names in other languages:
       •        Procedures
       •        Functions
       •        Subroutines
       •        Behaviors

In object-oriented programming, methods are called on an object using the dot notation. In example below, the
String at reference s was asked to give a substring of itself by calling the substring() method on the object.


String sub = s.substring(4,10);


Another way to think of the dot is “Call the method on the object that this reference is pointing to.”
















Notice that this method takes two integer parameters to work: a starting character index and an ending
character index. Some methods take no parameters to work. Parameters to methods are listed in the
parenthesis after the method name. The method trim(), for example, does not require any parameters in order
to do its job of removing leading and trailing whitespace.

Methods may or may not return a value. Notice that another String reference (sub) is assigned to s.substring
(4,10). The substring() method returns another String object. All of the methods shown in the previous
examples return some other object or a primitive. However, not all methods return something. For example,
the close() method on a database connection object (discussed later) returns void (nothing). Remember the
method main from the last chapter? What did main return?

As mentioned at the start of this chapter, objects are software bundles of data and related procedures. As you
now know, procedures are called methods in Java. However, objects are not limited to executing methods.
Objects also contain data. In Java, data is contained in
instance variables.

Instance variables go by many names, depending on your background or what resource you may be reading:
       •        Attributes
       •        Properties
       •        Fields
       •        Member variables
       •        State

Consider a Point object to see how to use an object’s instance variables. The Point, as you can probably
guess, represents a point in two-dimensional space.















A Point has two integer parts: x and y.

The code below creates a Point reference called “myPoint” and then accesses the x and y values stored inside
this object. In other words, it sets the x and y instance variables in the Point object.


Point myPoint = new Point();
myPoint.x = 2;
myPoint.y = 5;

















Remember that because a Point is an object, it has more than just data. It also has methods. You can tell if
you are referring to a field or calling a method based on whether parentheses ( ) are used after the dot. Fields
do not use parentheses. Method calls must use parentheses, even if no arguments are passed to the method.

int badge = agent.id;       //Accesses the 'id' instance variable.
agent.name = "Marcus"       //Changes the 'name' inst var to "Marcus".
Data secret = agent.spy();  //Calls the 'spy()' method, which returns
                           //some type of Data object.
agent.abortMission(false);  //Calls the 'abortMission()' method, which
                           //takes in a boolean argument.
agent.showBadge();          //Calls the 'showBadge()' method, which
                           //takes no arguments.
Stack and Heap
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services