Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie.

Similar presentations


Presentation on theme: "Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie."— Presentation transcript:

1 Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie

2 Primitives & Wrappers Java has a wrapper class for each of the eight primitive data types: Primitive Type Wrapper Class Primitive Type Wrapper Class booleanBooleanfloatFloat byteByteintInteger charCharacterlongLong doubleDoubleshortShort

3 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: List a = new ArrayList (); methodRequiringListOfIntegers(a);

4 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 ” ); Long n1 = Long.valueOf(42000000L); Long n1 = Long.valueOf( “ 42000000L ” );

5 Object => Value Each wrapper class Type has a method typeValue to obtain the object’s value: Integer i1 = Integer.valueOf(42); Boolean b1 = Boolean.valueOf( “ false ” ); System.out.println(i1.intValue()); System.out.println(b1.intValue()); => 42 false

6 String => value The Wrapper class for each primitive type has a method parseType() to parse a string representation & return the literal value. Integer.parseInt(“42”)=> 42 Boolean.parseBoolean(“true”)=> true Double.parseDouble(“2.71”)=> 2.71 //… Common use: Parsing the arguments to a program:

7 Parsing argument lists // Parse int and float program args. public parseArgs(String[] args) { for (int i = 0; i < args.length; i++) { try { …println(Integer.parseInt(args[i])); } catch (Exception e) { try { …println(Float.parseFloat(args[i])); } finally { } }}}

8 Parsing argument lists => arg # 0 = 0 arg # 1 = 42 arg # 2 = 999 arg # 3 = 0.0 arg # 4 = 1.42 arg # 5 = 9.0008

9 Sample values: boolObj new Boolean(Boolean.TRUE); charObj = new Character('a'); byteObj = new Byte("100"); shortObj = new Short("32000"); intObj = new Integer(2000000); longObj = new Long(500000000000000000L); floatObj = new Float(1.42); doubleObj = new Double(1.42); printWrapperInfo(); //method to print objects above

10 Sample values (output from previous slide): => For Boolean & Character Wrappers: Boolean:true Character:a For Number wrappers: Byte:100 Short:32000 Integer:2000000 Long:500000000000000000 Float:1.42 Double:1.42

11 Each Number Wrapper has a MAX_VALUE constant: byteObj = new Byte(Byte.MAX_VALUE); shortObj = new Short(Short.MAX_VALUE); intObj = new Integer(Integer.MAX_VALUE); longObj = new Long(Long.MAX_VALUE); floatObj = new Float(Float.MAX_VALUE); doubleObj = new Double(Double.MAX_VALUE); printNumValues("MAXIMUM NUMBER VALUES:");

12 MAX values (output from previous slide): => Byte:127 Short:32767 Integer:2147483647 Long:9223372036854775807 Float:3.4028235E38 Double:1.7976931348623157E308

13 Many useful utility methods, e.g., for Integer: int hashCode() static int numberOfLeadingZeros(int i) static int numberOfTrailingZeros(int i) static int reverse(int i) static int reverseBytes(int i) static int rotateLeft(int i, int distance) static int rotateRight(int i, int distance) static String toBinaryString(int i) static String toHexString(int i) static String toOctalString(int i) static String toString(int i, int radix)

14 Double & Float: Utilities for Arithmetic Operations: Constants POSITIVE_INFINITY & NEGATIVE_INFINITY Constant NaN = Not-a-Number (NaN) value. Methods isNaN(), isInfinite()

15 Primitive - Wrapper Split Personality: Although the Wrapper classes provide needed functionality (OO versions of primitives and supporting functionality), Java code is sometimes overly complicated due to the necessary conversions between the primitive and wrapper versions of data being manipulated. Joshua Bloch published a technical note, excerpts of which are used below. Bloch’s article can be found at http://java.sun.com/features/2003/05/bloch_qa.html http://java.sun.com/features/2003/05/bloch_qa.html

16 Bloch’s counting program: Java 1.4 Version public class Freq { private static final Integer ONE = new Integer(1); public static void main(String args[]) { // Maps word (String) to frequency (Integer) Map m = new TreeMap(); for (int i=0; i<args.length; i++) { Integer freq = (Integer) m.get(args[i]); m.put(args[i], (freq==null ? ONE : new Integer(freq.intValue() + 1))); } System.out.println(m); }

17 Notes on the 1.4 Version: This program generates a frequency table of words on the command line. It uses a Map whose keys are the words and whose values are the number of times that each word occurs on the line. The inner-loop code is a bit convoluted. Bloch continues by writing the same program with autoboxing, generics, and an enhanced for loop:

18 Bloch’s counting program: Java 1.5 Version: Bloch’s implementation rewritten with autoboxing, generics, and an enhanced for loop: public class Freq { public static void main(String args[]) { Map m = new TreeMap (); for (String word : args) { Integer freq = m.get(word); m.put(word, (freq == null ? 1 : freq + 1)); } System.out.println(m); }

19 Notes on the 1.5 Version: This version is much clearer, and is a good example of the use of a Wrapper class without the pitfalls of convoluted primitive-wrapper conversions.

20 Final Notes Java’s wrapper classes are useful and provide a great deal of functionality, well beyond that of the primitive types. Code using Wrapper classes and primitives can be convoluted. Use Java 1.5’s generics, autoboxing and the enhanced for loop to avoid unclear code.


Download ppt "Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie."

Similar presentations


Ads by Google