/*
 * AutoBoxTest.java
 *
 * Created on June 7, 2004, 11:48 AM
 */
import java.util.ArrayList;
/**
 *
 * @author  cpresser
 */
public class AutoBoxTest {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Create code to automatically convert primitives to objects
        //autoboxing of the number 5
        Integer number = 5;
        System.out.println(number);
        //auto unboxing of the Integer number and use in an expression
        int prim = number * 3;
        System.out.println(prim);
        
        //an expression using only the objects--cool
        Integer coeff = 6;
        Integer result = number * coeff;
        System.out.println(result);
        
        //storing int's in an ArrayList as Integers
        //use new generics
        //autoboxing will work without the generice <Integer> info, 
        //but auto unboxing (the second loop) will no compile
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < 10; i++){
            list.add(i);
        }
        
        //getting Integers out of the list and treating them as ints
        int sum = 0;
        /* old loop
        for(int j = 0; j < list.size(); j++){
            sum = sum + list.get(j);
        }
         */
        //new loop 
        //note: for(Integer j: list) works too!
        for(int j: list){
            sum = sum + j;
        }
        System.out.println(sum);

	//method call test--wrapping parameters and return values
	int intValue = 5;
	int primResult = objNegate(intValue);
	System.out.println(primResult);

	primResult = primNegate(intValue);
	System.out.println(primResult);

	Integer objValue = -7;
	Integer objResult = objNegate(objValue);
	System.out.println(objResult);

	objResult = primNegate(objValue);
	System.out.println(objResult);

    }

    //fortunately Integer objects are immutable, so the fact that it is
    //called by reference causes little trouble.
    public static Integer objNegate(Integer n){
	return -n;
    }

    public static int primNegate(int n){
	return -n;
    }
    
}
