Arrays of Objects Fall 2012 CS2302: Programming Principles
Wrappers: Java’s Wrapper classes for the primitive types Contents Wrappers: Java’s Wrapper classes for the primitive types Object class Arrays of Object Fall 2012 CS2302: Programming Principles
Java has a wrapper class for each of the eight primitive data types: Primitives & Wrappers Java has a wrapper class for each of the eight primitive data types: Primitive Type Wrapper Class boolean Boolean float Float byte Byte int Integer char Character long Long double Double short Short Fall 2012 CS2302: Programming Principles
Primitives vs. Wrappers int x = 25; Integer y = new Integer(33); Fall 2012 CS2302: Programming Principles
Use of the Wrapper Classes Java’s primitive data types (boolean, int, etc.) are not classes. Wrapper classes are used in situations where objects are required, such as for elements of a Collection: ArrayList<Integer> a = new ArrayList<Integer>(); a.add(new Integer(2)); Fall 2012 CS2302: Programming Principles
Value => Object: Wrapper Object Creation Wrapper.valueOf() takes a value (or string) and returns an object of that class: Integer i1 = Integer.valueOf(42); Integer i2 = Integer.valueOf(“42”); Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf(“true”); Fall 2012 CS2302: Programming Principles
Object => Value Each wrapper class Type has a method typeValue to obtain the object’s value: Integer i1 = Integer.valueOf(42); System.out.println(i1.intValue()); Boolean b1 = Boolean.valueOf(“false”); System.out.println(b1.booleanValue()); => 42 false Fall 2012 CS2302: Programming Principles
The Object Class and Its Methods java.lang.Object class The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. Circle c1 = new Circle(); System.out.println(c1.toString()); Fall 2012 CS2302: Programming Principles
The elements of an array can be object references Array of Object The elements of an array can be object references Declaration: ArrayList<Object> arrayRefVar; Fall 2012 CS2302: Programming Principles
Declaring and Creating in one step: Basic Operations Creation: arrayRefVar = new ArrayList<Object>(); Declaring and Creating in one step: ArrayList<Object> myList = new ArrayList<Object>(); Fall 2012 CS2302: Programming Principles
Class definition public class PartialArrayOfObject { /*Where to store the data.*/ private ArrayList<Object> data; /* Constructor: Initialize the array to the given size. This will be the maximum number that can be held.*/ public PartialArrayOfObject(int size) { data = new ArrayList<Object>(); numStored = 0; } /*Insert the string val into the array so that it ends up with the given*/ public void insertAt(Object val, int index) { if(index < 0 || index > numStored) { System.out.println(“Insert index out of bounds”); } else if(numStored >= data.length) {// no more room System.out.println("Partial array is full"); }else { for(int j = numStored; j > index; j--) { data[j] = data[j-1]; data.add(index,val); // put the new value in place Fall 2012 CS2302: Programming Principles
Create an integer array public class Test { public static void main(String[] args) { /*create an object array with size 5*/ PartialArrayOfObject intArr = new PartialArrayOfObject(); /*Fill the array and display each element*/ for(int i=0; i<5;i++){ intArr.add(new Integer(i)); System.out.println(“Element " + i + “ is: ” + intArr.get(i)); } /*Calculate total*/ int total = 0; total += (Integer)intArr.get(i); System.out.println("Total is " + total); Fall 2012 CS2302: Programming Principles