Review 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:

Any type of data at all:

  • Primitive types, such as short, int, double
  • Object references.

We have not used arrays of object references so far in these notes. They will be discussed in a future chapter.

Review Program

Here is a short program: Here is a description of the action:
// Review Program
//
class Alteration
{
  void zero ( int x )
  {
    x = 0;
  }
}

class AlterTest
{
  public static void main ( String[] args )
  {
    Alteration alt = new Alteration();
    int value = 27;
    System.out.println( "Before:" + value );
    
    alt.zero( value );
    System.out.println( "After:"  + value );
  }
}
  1. The program starts running with the static main() method.
  2. An Alteration object is constructed.
    • The default constructor is used, since the class Alteration does not define a constructor.
    • The Alteration object contains the zero() method.
  3. The primitive int variable value is initialized to 27.
  4. The number currently held in value is printed out.
    • Before: 27 is printed
  5. The zero() method is called with value as a parameter.
    • Think about what happens next. It is easy to make a mistake here.
  6. The number currently held in value is printed out.
    • What is printed out?

QUESTION 2:

What is printed out?