/*
  This program demonstrates the usage of Integer and String classes. 
*/

public class WrapperDemo {
    public static void main(String[] args){	
	/* Integer wrapper class demo */ 
	Integer I = new Integer(42);   // creates a new Integer object with value 42
	int i = I.intValue();          // convert it to int primitive type
	i++;                           // increment i
	
	// int i is converted to string and concatenated to "i++ =".
	System.out.println("i++ = " + i);    
	
	// same as the previous statement. This one is better in terms of performance 
	System.out.println("i++ = " + Integer.toString(i));       

	// The below System.out.println statements use toString() method for printing. 
	// directly concatenating the below variables will indirectly call this method
	// but there will be overhead of creating intermediary objects.  
	System.out.println("I = " + I.toString() + "\n");

	// alternatively parseInt can be called to directly convert a String to primitive type int
	if(args.length > 0) {
	    try {
		int argInt = Integer.parseInt(args[0]); 
		argInt++;
		System.out.println(argInt + "\n");
	    }
	    catch(Exception e) {
		System.out.println("You must enter a number");
	    }
	}
	

	/* String class demo */ 
	String str = "text";
	System.out.println("Test string: " + str + "\n"); 
	
	char x = str.charAt(2);		
	System.out.println("charAt(2) = " + Character.toString(x) + "\n");		
	
	int len = str.length();		
	System.out.println("length() = " + Integer.toString(len) + "\n");		
	
	System.out.println("str.equals(\"text\"): " + str.equals("text") + "\n");

	i = str.compareTo("string");
	System.out.println("compareTo(\"string\") :" + Integer.toString(i) + "\n"); 
	
	i = str.compareTo("word");
	System.out.println("compareTo(\"word\") : " + Integer.toString(i) + "\n");
	
	i = str.compareTo("text");
	System.out.println("compareTo(\"text\") : " + Integer.toString(i) + "\n");				
    }
}
