Review

Java Identifiers

Identifiers in Java (and many other languages) are strings which contain mixtures of letters (both upper and lower case), digits and underscores starting with a letter or underscore (not a digit!). For example: x, xYZ, a23, B23_, __3_A, etc. Based on their usage within Java programs there are certain conventions which should be (but are not required to be) followed. For the most part it is a good idea to use English words or abbreviations as the basis of identifiers in order to be more descriptive about what the identifier represents.

Common terms describing identifiers are these: Underscores are also common in local variable names and parameters, e.g.,
int my_number = 33;  // local variable
int _the_parameter   // method parameter
The conventions (at this point) are these:

Methods

Recall that a method is nothing more than a function within a class. In some programming languages, a function can be at the file level, so to speak, not embedded in another structure. However in Java, functions only appear within classes and for that reason are called methods.

In this first example a method is used and and arguments are successively passed into the method by through a parameter.

PassArg.java
/**
   This program demonstrates a method with a parameter.
*/
 
public class PassArg
{
   public static void main(String[] args)
   {
      int x = 10;
 
      System.out.println("I am passing values to displayValue.");
      displayValue(5);                       // Pass 5
      displayValue(x);                       // Pass 10
      displayValue(x * 4);                   // Pass 40
      displayValue(Integer.parseInt("700")); // Pass 700
      System.out.println("Now I am back in main.");
   }
 
   /**
      The displayValue method displays the value
      of its integer parameter.
   */
 
   public static void displayValue(int num)
   {
      System.out.println("The value is " + num);
   }
}
select

Pass by value

The next example illustrates the pass by value aspect of parameter passing for Java primitive types. Specifically, pass by value means that the parameter is a separate copy of the argument.

PassByValue.java
/**
   This program demonstrates that only a copy of an argument
   is passed into a method.
*/
 
public class PassByValue
{
   public static void main(String[] args)
   {
      int number = 99; // number starts with 99
 
      // Display the value in number.
      System.out.println("number is " + number);
 
      // Call changeMe, passing the value in number
      // as an argument.
      changeMe(number);
 
      // Display the value in number again.
      System.out.println("number is " + number);
   }
 
   /**
      The changeMe method accepts an argument and then
      changes the value of the parameter.
   */
 
   public static void changeMe(int myValue)
   {
      System.out.println("I am changing the value.");
 
      // Change the myValue parameter variable to 0.
      myValue = 0;
 
      // Display the value in myValue.
      System.out.println("Now the value is " + myValue);
   }
}
select
During the method execution, the argument, number, and the parameter myValue have separate existences. Initially we have:
number: 
99
myValue:
99
after the assignment statement within the method we have:
number: 
99
myValue:
0
The outcome is that number is left intact. The same happens for all primitive types, such as double and char.

Pass by reference by value

The Java String is actually an object and all objects we'll study have many of the behaviors of the String. A String consists of two separate parts: the reference (or pointer) and the content. The method in which objects are passed can be called pass by-reference-by-value. Only the reference is copied, not the contents.

Here is the sample program:

PassString.java
/**
   This program demonstrates that String arguments
   cannot be changed.
*/
 
public class PassString
{
   public static void main(String[] args)
   {
      // Create a String object containing "Shakespeare".
      // The name variable references the object.
      String name = "Shakespeare";
 
      // Display the String referenced by the name variable.
      System.out.println("In main, the name is " + name);
 
      // Call the changeName method, passing the
      // contents of the name variable as an argument.
      changeName(name);
 
      // Display the String referenced by the name variable.
      System.out.println("Back in main, the name is " + name);
   }
 
   /**
      The changeName method accepts a String as its argument
      and assigns the str parameter to a new String.
   */
 
   public static void changeName(String str)
   {
      // Create a String object containing "Dickens".
      // Assign its address to the str parameter variable.
      str = "Dickens";
 
      // Display the String referenced by str.
      System.out.println("In changeName, the name " + "is now " + str);
   }
}
select
In this example, we have:
name: 
ref
Shakespeare
The actual value of ref is unimportant, we just say it "points to" the actual sequence of characters, held somewhere is memory. When the argument is passed to the method, the reference is copied into the parameter, i.e.,
name: 
ref
str:
ref
Shakespeare
str:
ref
After the assignment with the method, the references must be distinct, i.e.,
name: 
ref
Shakespeare
str:
ref1
Dickens
With Strings and objects in general, there is a potential that the method could modify the original content. However, for Strings, this is impossible; Strings are said to be immutable.


© Robert M. Kline