Wrappers Javas Wrapper Classes for the Primitives Types

  • Slides: 20
Download presentation
Wrappers: Java’s Wrapper Classes for the Primitives Types By Shubhangi K. Patil Head, Department

Wrappers: Java’s Wrapper Classes for the Primitives Types By Shubhangi K. Patil Head, Department of Computer Science

Primitives & Wrappers Java has a wrapper class for each of the eight primitive

Primitives & Wrappers Java has a wrapper class for each of the eight primitive data types: Primitive Type Wrapper Class boolean byte char double Boolean Byte Character Double float int long short Float Integer Long Short

Use of the Wrapper Classes Java’s primitive data types (boolean, int, etc. ) are

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<Integer> a = new Array. List<Integer>(); method. Requiring. List. Of. Integers(a);

Value => Object: Wrapper Object Creation Wrapper. value. Of() takes a value (or string)

Value => Object: Wrapper Object Creation Wrapper. value. Of() takes a value (or string) and returns an object of that class: Integer i 1 = Integer. value. Of(42); Integer i 2 = Integer. value. Of(“ 42”); Boolean b 1 = Boolean. value. Of(true); Boolean b 2 = Boolean. value. Of(“true”); Long n 1 = Long. value. Of(42000000 L); Long n 1 = Long. value. Of(“ 42000000 L”);

Object => Value Each wrapper class Type has a method type. Value to obtain

Object => Value Each wrapper class Type has a method type. Value to obtain the object’s value: Integer i 1 = Integer. value. Of(42); Boolean b 1 = Boolean. value. Of(“false”); System. out. println(i 1. int. Value()); System. out. println(b 1. int. Value()); => 42 false

String => value The Wrapper class for each primitive type has a method parse.

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

Parsing argument lists // Parse int and float program args. public parse. Args(String[] args)

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

Parsing argument lists => arg # 0 = 0 arg # 1 = 42

Parsing argument lists => arg # 0 = 0 arg # 1 = 42 arg # 2 = 999 arg # 3 = 0. 0 arg # 4 = 1. 42 arg # 5 = 9. 0008

Sample values: bool. Obj new Boolean(Boolean. TRUE); char. Obj = new Character('a'); byte. Obj

Sample values: bool. Obj new Boolean(Boolean. TRUE); char. Obj = new Character('a'); byte. Obj = new Byte("100"); short. Obj = new Short("32000"); int. Obj = new Integer(2000000); long. Obj = new Long(5000000000 L); float. Obj = new Float(1. 42); double. Obj = new Double(1. 42); print. Wrapper. Info(); //method to print objects above

Sample values (output from previous slide): => For Boolean & Character Wrappers: Boolean: true

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

Each Number Wrapper has a MAX_VALUE constant: byte. Obj = new Byte(Byte. MAX_VALUE); short.

Each Number Wrapper has a MAX_VALUE constant: byte. Obj = new Byte(Byte. MAX_VALUE); short. Obj = new Short(Short. MAX_VALUE); int. Obj = new Integer(Integer. MAX_VALUE); long. Obj = new Long(Long. MAX_VALUE); float. Obj = new Float(Float. MAX_VALUE); double. Obj = new Double(Double. MAX_VALUE); print. Num. Values("MAXIMUM NUMBER VALUES: ");

MAX values (output from previous slide): => Byte: 127 Short: 32767 Integer: 2147483647 Long:

MAX values (output from previous slide): => Byte: 127 Short: 32767 Integer: 2147483647 Long: 9223372036854775807 Float: 3. 4028235 E 38 Double: 1. 7976931348623157 E 308

Many useful utility methods, e. g. , for Integer: static static static int int

Many useful utility methods, e. g. , for Integer: static static static int int String hash. Code() number. Of. Leading. Zeros(int i) number. Of. Trailing. Zeros(int i) reverse. Bytes(int i) rotate. Left(int i, int distance) rotate. Right(int i, int distance) to. Binary. String(int i) to. Hex. String(int i) to. Octal. String(int i) to. String(int i, int radix)

Double & Float: Utilities for Arithmetic Operations: Constants POSITIVE_INFINITY & NEGATIVE_INFINITY Constant Na. N

Double & Float: Utilities for Arithmetic Operations: Constants POSITIVE_INFINITY & NEGATIVE_INFINITY Constant Na. N = Not-a-Number (Na. N) value. Methods is. Na. N(), is. Infinite()

Primitive - Wrapper Split Personality: Although the Wrapper classes provide needed functionality (OO versions

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

Bloch’s counting program: Java 1. 4 Version public class Freq { private static final

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 Tree. Map(); 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. int. Value() + 1))); } System. out. println(m); } }

Notes on the 1. 4 Version: This program generates a frequency table of words

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:

Bloch’s counting program: Java 1. 5 Version: Bloch’s implementation rewritten with autoboxing, generics, and

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<String, Integer> m = new Tree. Map<String, Integer>(); for (String word : args) { Integer freq = m. get(word); m. put(word, (freq == null ? 1 : freq + 1)); } System. out. println(m); } }

Notes on the 1. 5 Version: This version is much clearer, and is a

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.

Final Notes Java’s wrapper classes are useful and provide a great deal of functionality,

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.