The Java Program: PassString.java

  1 // PassString.java
  2 
  3 import java.lang.String;
  4 import java.lang.StringBuilder;
  5 
  6 class PassString {
  7 
  8    public static void main (String[] args) {
  9 
 10       /*
 11         Whether "s" or "b" are final or not, it makes no difference
 12         in the output.  They are, in fact, assigned but once, so they
 13         both ought to be declared final.
 14       */
 15  
 16       String s = "abc";  
 17       System.out.println ("before: s = " + s);
 18       mangle (s);
 19       System.out.println ("after:  s = " + s);
 20 
 21       final StringBuilder b = new StringBuilder ("abc");
 22       System.out.println ("before: b = " + b);
 23       mutilate (b);
 24       System.out.println ("after: b = " + b);
 25    }
 26 
 27    public static void mangle (String arg) {
 28       arg = "xyz";
 29       System.out.println ("mangle: arg = " + arg);
 30    }
 31 
 32    public static void mutilate (StringBuilder arg) {
 33       arg.setCharAt (1,'y');
 34       System.out.println ("mutilate: arg = " + arg);
 35    }
 36 }