Review:    caveat when using ArrayList

  • The ArrayList must be used with an class (= reference type)

    I.e.:

    • An ArrayList cannot be used to store data of a primitive data type (such as int, double, etc)

  • This program will generate a compile error:

    import java.util.ArrayList;
    
    public class myProg
    {
        public static void main(String[] args)
        {
            ArrayList<int> myList = new ArrayList<>(); // Compile error
        }
    }


  • We can solve this problem with classes that contain a variable of a primitive type (a.k.a.: a "wrapper" class)

Making a int primitive data type variable into an object

  • We can "package" an int primitive data types variable into an object as follows:

     public class IntegerObj  // A "wrapper" class
     {
         public int value;    // contains a variable of some primitive type
    
         // Constructor
         public IntegerObj(int x)
         {
             value = x;
         }
     }
    

  • Now we can create int objects:

     IntegerObj a = new IntegerObj(4);
    
     System.out.println( a.value ); 
    

DEMO: demo/12-wrapper/01-intro/Demo1.java + IntegerObj.java

ArrayList with "packaged" primitive data types

  • We can use the IntegerObj class to create an ArrayList of "integers":

     import java.util.ArrayList;
    
     public class Demo2
     {
         public static void main(String[] args)
         {
             ArrayList<IntegerObj> myList = new ArrayList<>();
            
             myList.add( new IntegerObj(7) ); 
    	 myList.add( new IntegerObj(4) );
    	 myList.add( new IntegerObj(9) );
    	 System.out.println( myList ); 
         }
     }

DEMO: demo/12-wrapper/01-intro/Demo2.java + IntegerObj.java

Wrapper classes

  • Because Java has many library classes that only works with reference types (and do not work with primitive data types):

    • The Java library provides classes (called Wrapper classes) that only contains a variable of a primitive data type

      (Just like the IntegerObj class in the previous example)

  • The Wrapper classes in Java are:

      Primitive Data Type 	     Corresponding Wrapper Class
     ============================================
      byte 	                     Byte
      short			     Short
      int			     Integer
      long			     Long
      float			     Float
      double		     Double
      boolean		     Boolean
      char			     Character

Example using a Wrapper class   Integer

  • The wrapper class for the primitive type int is:

    • java.lang.Integer (online documentation: )

  • We can create an ArrayList object that store int values using the wrapper class Integer:

    public class IntegerWrapper
    {
        public static void main(String[] args)
        {
            ArrayList<Integer> myList = new ArrayList<>();
            
            myList.add( new Integer(7) ); 
    	myList.add( new Integer(4) );
    	myList.add( new Integer(9) );
    
    	System.out.println( myList ); 
        }
    }

DEMO: demo/12-wrapper/01-intro/IntegerWrapper.java

Another example using a Wrapper class   Double

  • If you want to store double typed values in an ArrayList object, then you can use the Double wrapper class:

    import java.util.ArrayList;
    
    public class myProg
    {
        public static void main(String[] args)
        {
            ArrayList<Double> myList = new ArrayList<>();
            
            myList.add( new Double(7.0) ); 
    	myList.add( new Double(4.0) );
    	myList.add( new Double(9.0) );
    
    	System.out.println( myList ); 
        }
    }

DEMO: demo/12-wrapper/01-intro/DoubleWrapper.java

Other things defined inside wrapper classes

  • In addition to the primitive type variable, wrapper classes also contain:

    • Maximum and Minimum range constants

      • These constants denote the largest and the smallest value that a variable of this primitive type can store

    • Constructors

      • Used to initialize the primitive type variable

    • Accessor methods

      • These methods will return the value of the primitive variable in the wrapper class object

    • Conversion methods

      • These methods convert a number string to the primitive type representation and vice versa

Other things defined inside wrapper classes:   Integer

  • Example content of the Integer wrapper class

    Other wrapper classes have similar content

Other things defined inside wrapper classes:   Double

  • Example content of the Double wrapper class

    Other wrapper classes have similar content

The MAX_VALUE and MIN_VALUE range constants in a wrapper class

  • Every wrapper class has 2 range constants:

      MAX_VALUE:  the largest  value that can be stored
    
      MIN_VALUE:  the smallest value that can be stored

    Example: the MAX_VALUE and MIN_VALUE in the Integer and Double wrapper classes

        public static void main(String[] args)
        {
            System.out.println( Integer.MAX_VALUE ); //  2147483647
            System.out.println( Integer.MIN_VALUE ); // −2147483648
    
    	System.out.println( Double.MAX_VALUE ); //  1.8 e308
            System.out.println( Double.MIN_VALUE ); //  4.9 e-324
        }

DEMO: demo/12-wrapper/01-intro/RangeConstants.java

The constructors in wrapper classes

  • Every wrapper class has 2 constructors:

      Constructor 1:  used to create an object 
                      from a primitive typed value
    
      Constructor 2:  used to create an object 
                      from a String

    Example: Integer wrapper class

        public static void main(String[] args)
        {
            Integer i1 = new Integer(99);   // Constructor 1
            Integer i2 = new Integer("44"); // Constructor 2
            
            System.out.println( i1 );
            System.out.println( i2 );
        }

DEMO: demo/12-wrapper/01-intro/Constructors.java

The accessor method in wrapper classes

  • Every wrapper class has an accessor method that returns the value in its primitive type:

      accessor method:  returns the primitive typed value 
                        stored inside the wrapper object 

    Example: Integer wrapper class

        public static void main(String[] args)
        {
            Integer i1 = new Integer(99);   // Constructor 1
            Integer i2 = new Integer("44"); // Constructor 2
            
    	int x1 = i1.intValue();   // Call accessor method
    	int x2 = i2.intValue();   // Call accessor method
    
            System.out.println( x1 );
            System.out.println( x2 );
        }

DEMO: demo/12-wrapper/01-intro/Accessor.java

The String conversion method in wrapper classes

  • Every wrapper class has a static method that converts a String to that primitive type:

      parsePrimType(): converts a String to a value of that primitive type  

    Example: the Integer.parseInt() in the Integer wrapper class

        public static void main(String[] args)
        {
            String s = "44";
            int i = Integer.parseInt(s); // Converts String to int
    
            System.out.println( s + 1 );  // 441
            System.out.println( i + 1 );  // 45
        }

  • Comment:

    • We have used Integer.parseInt( ) before to read in a number string and converts it to an int for computation (see )

Important fact: wrapper class objects are immutable

  • Wrapper class objects are immutable

    Because:   a wrapper class does not contain any mutator methods:

    The instance variable (value) is private, therefore, it cannot be updated !!!