Lecture 8 Wrapper Classes, Arrays
Lecture 8 Wrapper Classes, Arrays
CIC-212
Java Programming
Lecture
Java’s Wrapper Classes for the Primitives Types,
Arrays and Collections
Primitives & Wrappers
Java has a wrapper class for each of the eight primitive data types.
Wrapper classes are available in java.lang package
UNBOXING: Wrapper object will be converted back to a primitive data type, and this process is
called unboxing. The Number class is part of the java.lang package.
public class Test
{
public static void main(String args[])
Integer i=new Integer(10); {
Integer x = 5; // boxes int to an
Integer object
int a=i.intValue(); x = x + 10; // unboxes the Integer to
a int
System.out.println(x);
}
}
Use of the Wrapper Classes
1. Primitive data types are passed by value. Objects are passed
by reference. Wrapper classes facilitate passing primitives by
reference, as an argument to a method.
2. Wrapper classes provide many ready to use utility methods.
3. The classes in java.util package handles only objects and
hence wrapper classes help in this case also.
4. Data structures in the Collection framework, such as
ArrayList, HashMap and Vector, store only objects (reference
types) and not primitive types.
5. An object is needed to support synchronization in
multithreading.
Converting String/value to
Objects
Wrapper.valueOf() takes a value (or string)
and returns an object of that class:
Integer i1 = Integer.valueOf(42);
Integer i2 = Integer.valueOf(“42”);
Long n1 = Long.valueOf(42000000L);
Long n1 = Long.valueOf(“42000000L”);
Accessing values from the Objects
int i = i1.intValue();
boolean b = b1.booleanValue();
long l = l1.longValue();
42
false
42000000
Converting String to Primitive type
The Wrapper class for each primitive type has a method parseType() to
parse a string representation & return the literal value.
Declaring an array
int[] myArray;
int[] myArray = new int[5];
String[] stringArray = new String[10];
String[] strings = new String[] {“one”, “two”};
• This class contains various methods for manipulating arrays (such as sorting and
searching).
• The methods in this class throw a NullPointerException if the specified array reference is
null.
static int binarySearch(byte[] a, byte key) This method searches the specified array of bytes
for the specified value using the binary search algorithm.
static boolean equals(long[] a, long[] a2) This method returns true if the two specified arrays
of longs are equal to one another.
static int hashCode(byte[] a) This method returns a hash code based on the contents of the
specified array.
static void sort(char[] a) This method sorts the specified array of chars into ascending
numerical order.
Around 105 methods defined in Arrays class to do different operations on arrays.
Java Arrays – Sorting
Advantages
• Very flexible, can hold any kind of object
Disadvantages
• Not as efficient as arrays (for some uses)
• Not type-safe. Store references to Object
Java Collections
Use get()
Note that we have to cast the object to its original
type.
• Collections…
String s = (String)myList.get(1); //get first element
String s2 = (String)myList.get(10); //get tenth element
• Maps…
String s = (String)myMap.get(“google”);
String s2 = (String)mpMap.get(“yahoo”);
Collections – Getting all
items
For Lists, we could use a for loop, and loop through the list to
get() each item.
Method Description
boolean add(object) It is used to insert the specified element into this queue
and return true upon success.
Object element() It is used to retrieves, but does not remove, the head of
this queue.
Object peek() It is used to retrieves, but does not remove, the head of
this queue, or returns null if this queue is empty.