//: PassByValue.java
import java.util.*;
/**
 * Successful pass-by-value with clone()
*/
class Value implements Cloneable {
  int i;
  Value(int ii) { i = ii; }
  public Object clone() {
    Object o = null;
    try {
      o = super.clone();
    } catch (CloneNotSupportedException e) {
      System.out.println("Value can't clone");
    }
    return o;
  }
  public String toString() {
    return Integer.toString(i);
  }
}

public class PassByValue {
  static Value g(Value v) {
    // Pass by reference,
    // modifies outside object:
    v.i++;
    return v;
  }
  static Value f(Value v) {
    v = (Value)v.clone(); // Pass by value
    v.i++;
    return v;
  }
  public static void main(String args[]) {
    Value a = new Value(11);
    Value b = g(a);
    // Testing object equivalence,
    // not value equivalence:
    if(a == b) 
      System.out.println("a == b");
    else 
      System.out.println("a != b");
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    Value c = new Value(47);
    Value d = f(c);
    if(c == d) 
      System.out.println("c == d");
    else 
      System.out.println("c != d");
    System.out.println("c = " + c);
    System.out.println("d = " + d);
  }
} ///:~