A String Object

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Do you thing 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

Objects are big, complicated, and vary in size. You DO NOT automatically get an object when you declare an object reference variable. All you get is a place to put a future object reference. Examine the following:

gingham dog pillow
class EgString 
{
  public static void main ( String[] args )
  {
    String str;
    
    str = new String( "The Gingham Dog" );

    System.out.println( str );
  }
}

An object contains data and methods (state and behavior). You can visualize the String object in the above program like this:

gingham dog object

The data section of the object contains the characters. The methods section of the object contains many methods. (Actually, this picture is a simplification. The Java system does something more efficient, but logically equivalent.) Future diagrams sometimes will not show the methods of the object.

QUESTION 4:

(Review:) What does the following new operator do?

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