Another Example Program

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

Answer:

Yes. The method just returns a reference to the String itself. No new object is created.

Another Example Program

Here is the example program with yet another change:

import java.awt.*;
class PointEg3
{

  public static void main ( String arg[] )
  {
    Point a = new Point();              // declarations and construction combined
    Point b = new Point( 12, 45 );
    Point c = new Point( b );

    System.out.println( a ); // create a temporary String based on "a", print it out
    System.out.println( b ); // create a temporary String based on "b", print it out
    System.out.println( c ); // create a temporary String based on "c", print it out
  }
}

The program prints out:

java.awt.Point[x=0,y=0]
java.awt.Point[x=12,y=45]
java.awt.Point[x=12,y=45]

This program is deceptively short; its execution calls for quite a bit of activity.

QUESTION 11:

Just as the program is about to close, how many objects have been created?

How many object references are there?

Has any garbage been created?