go to previous page   go to home page   go to next page hear noise highlighting

Do you think that all objects of the same type are the same size?

Answer:

No. Objects are bigger and more complicated than primitive data types. It would be too restrictive if all objects of the same type had to be the same size.


A String Object


gingham dog pillow

public class EgString 
{
  public static void main ( String[] args )
  {
    String str;    //  statement 1
    
    str = new String( "The Gingham Dog" );   // statement 2

    System.out.println( str );
  }
}

Objects are big, complicated, and vary in size.

You DO NOT get an object when you declare an object reference variable. All you get is a place to put an object reference.

(You can combine variable declaration with object construction, but this must be done explicitly using a new operator.)

Examine the program.

Statement 1 creates an object reference variable. No object exists until statement 2 executes.

Statement 2 constructs an object (using new) and puts a reference to that object into the variable.

You can visualize the String object in the above program like this:


gingham dog object

An object contains data and methods (state and behavior). The data section of the object contains the characters. The methods section of the object contains many methods. (Actually, this picture is a simplification. Java does something more efficient but logically equivalent.)


QUESTION 4:

(Review:) What does the new operator do?

   
str = new String( "The Gingham Dog" );

go to previous page   go to home page   go to next page