Wrappers

1,266 views
1,250 views

Published on

Hariprasanna V (9843824677)

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,266
On SlideShare
0
From Embeds
0
Number of Embeds
263
Actions
Shares
0
Downloads
44
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Wrappers

  1. 1. Wrappers: Java’s Wrapper Classes for the Primitives Types Steve Bossie
  2. 2. Primitives & Wrappers <ul><li>Java has a wrapper class for each of the eight primitive data types: </li></ul>Double Character Byte Boolean Wrapper Class short long int float Primitive Type Short double Long char Integer byte Float boolean Wrapper Class Primitive Type
  3. 3. Use of the Wrapper Classes <ul><li>Java’s primitive data types (boolean, int, etc.) are not classes. </li></ul><ul><li>Wrapper classes are used in situations where objects are required, such as for elements of a Collection: </li></ul><ul><li>List<Integer> a = new ArrayList<Integer>(); </li></ul><ul><li>methodRequiringListOfIntegers(a); </li></ul>
  4. 4. Value => Object: Wrapper Object Creation <ul><li>Wrapper .valueOf() takes a value (or string) and returns an object of that class: </li></ul><ul><li>Integer i1 = Integer.valueOf(42); </li></ul><ul><li>Integer i2 = Integer.valueOf(“42”); </li></ul><ul><li>Boolean b1 = Boolean .valueOf(true); </li></ul><ul><li>Boolean b2 = Boolean .valueOf(“true”); </li></ul><ul><li>Long n1 = Long.valueOf(42000000L); </li></ul><ul><li>Long n1 = Long.valueOf(“42000000L”); </li></ul>
  5. 5. Object => Value <ul><li>Each wrapper class Type has a method typeValue to obtain the object’s value: </li></ul><ul><li>Integer i1 = Integer.valueOf(42); </li></ul><ul><li>Boolean b1 = Boolean.valueOf(“false”); </li></ul><ul><li>System.out.println(i1.intValue()); </li></ul><ul><li>System.out.println(b1.intValue()); </li></ul><ul><li>=> </li></ul><ul><li>42 </li></ul><ul><li>false </li></ul>
  6. 6. String => value <ul><li>The Wrapper class for each primitive type has a method parse Type () to parse a string representation & return the literal value. </li></ul><ul><li>Integer.parseInt(“42”) => 42 </li></ul><ul><li>Boolean.parseBoolean(“true”) => true </li></ul><ul><li>Double.parseDouble(“2.71”) => 2.71 </li></ul><ul><li>//… </li></ul><ul><li>Common use: Parsing the arguments to a program: </li></ul>
  7. 7. Parsing argument lists <ul><li>// Parse int and float program args. </li></ul><ul><li>public parseArgs(String[] args) { </li></ul><ul><li>for (int i = 0; i < args.length; i++) { </li></ul><ul><li>try { </li></ul><ul><li>… println(Integer. parseInt (args[i])); </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><li>try { </li></ul><ul><li>… println(Float. parseFloat (args[i])); </li></ul><ul><li>} finally { } </li></ul><ul><li>}}} </li></ul>
  8. 8. Parsing argument lists <ul><li>=> </li></ul><ul><li>arg # 0 = 0 </li></ul><ul><li>arg # 1 = 42 </li></ul><ul><li>arg # 2 = 999 </li></ul><ul><li>arg # 3 = 0.0 </li></ul><ul><li>arg # 4 = 1.42 </li></ul><ul><li>arg # 5 = 9.0008 </li></ul>
  9. 9. Sample values: <ul><li>boolObj new Boolean(Boolean.TRUE); </li></ul><ul><li>charObj = new Character('a'); </li></ul><ul><li>byteObj = new Byte(&quot;100&quot;); </li></ul><ul><li>shortObj = new Short(&quot;32000&quot;); </li></ul><ul><li>intObj = new Integer(2000000); </li></ul><ul><li>longObj = new Long(500000000000000000L); </li></ul><ul><li>floatObj = new Float(1.42); </li></ul><ul><li>doubleObj = new Double(1.42); </li></ul><ul><li>printWrapperInfo(); //method to print objects above </li></ul>
  10. 10. Sample values (output from previous slide): <ul><li>=> </li></ul><ul><li>For Boolean & Character Wrappers: </li></ul><ul><li>Boolean:true </li></ul><ul><li>Character:a </li></ul><ul><li>For Number wrappers: </li></ul><ul><li>Byte:100 </li></ul><ul><li>Short:32000 </li></ul><ul><li>Integer:2000000 </li></ul><ul><li>Long:500000000000000000 </li></ul><ul><li>Float:1.42 </li></ul><ul><li>Double:1.42 </li></ul>
  11. 11. Each Number Wrapper has a MAX_VALUE constant: <ul><li>byteObj = new Byte(Byte.MAX_VALUE); </li></ul><ul><li>shortObj = new Short(Short.MAX_VALUE); </li></ul><ul><li>intObj = new Integer(Integer.MAX_VALUE); </li></ul><ul><li>longObj = new Long(Long.MAX_VALUE); </li></ul><ul><li>floatObj = new Float(Float.MAX_VALUE); </li></ul><ul><li>doubleObj = new Double(Double.MAX_VALUE); </li></ul><ul><li>printNumValues(&quot;MAXIMUM NUMBER VALUES:&quot;); </li></ul>
  12. 12. MAX values (output from previous slide): <ul><li>=> </li></ul><ul><li>Byte:127 </li></ul><ul><li>Short:32767 </li></ul><ul><li>Integer:2147483647 </li></ul><ul><li>Long:9223372036854775807 </li></ul><ul><li>Float:3.4028235E38 </li></ul><ul><li>Double:1.7976931348623157E308 </li></ul>
  13. 13. Many useful utility methods, e.g., for Integer: <ul><li>int hashCode() </li></ul><ul><li>static int numberOfLeadingZeros(int i) </li></ul><ul><li>static int numberOfTrailingZeros(int i) </li></ul><ul><li>static int reverse(int i) </li></ul><ul><li>static int reverseBytes(int i) </li></ul><ul><li>static int rotateLeft(int i, int distance) </li></ul><ul><li>static int rotateRight(int i, int distance) </li></ul><ul><li>static String toBinaryString(int i) </li></ul><ul><li>static String toHexString(int i) </li></ul><ul><li>static String toOctalString(int i) </li></ul><ul><li>static String toString(int i, int radix) </li></ul>
  14. 14. Double & Float: Utilities for Arithmetic Operations: <ul><li>Constants POSITIVE_INFINITY & NEGATIVE_INFINITY </li></ul><ul><li>Constant NaN = Not-a-Number (NaN) value. </li></ul><ul><li>Methods isNaN(), isInfinite() </li></ul>
  15. 15. Primitive - Wrapper Split Personality: <ul><li>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. </li></ul><ul><li>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 </li></ul>
  16. 16. Bloch’s counting program: Java 1.4 Version <ul><li>public class Freq { </li></ul><ul><li>private static final Integer ONE = new Integer(1); </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>// Maps word (String) to frequency (Integer) </li></ul><ul><li>Map m = new TreeMap(); </li></ul><ul><li>for (int i=0; i<args.length; i++) { </li></ul><ul><li>Integer freq = (Integer) m.get(args[i]); </li></ul><ul><li>m.put(args[i], (freq==null ? ONE : </li></ul><ul><li>new Integer(freq.intValue() + 1))); </li></ul><ul><li>} </li></ul><ul><li>System.out.println(m); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 17. Notes on the 1.4 Version: <ul><li>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. </li></ul><ul><li>The inner-loop code is a bit convoluted. Bloch continues by writing the same program with autoboxing, generics, and an enhanced for loop: </li></ul>
  18. 18. Bloch’s counting program: Java 1.5 Version: <ul><li>Bloch’s implementation rewritten with autoboxing, generics, and an enhanced for loop: </li></ul><ul><li>public class Freq { </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>Map<String, Integer> m = new TreeMap<String, Integer>(); </li></ul><ul><li>for (String word : args) { </li></ul><ul><li>Integer freq = m.get(word); </li></ul><ul><li>m.put(word, (freq == null ? 1 : freq + 1)); </li></ul><ul><li>} </li></ul><ul><li>System.out.println(m); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. Notes on the 1.5 Version: <ul><li>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. </li></ul>
  20. 20. Final Notes <ul><li>Java’s wrapper classes are useful and provide a great deal of functionality, well beyond that of the primitive types. </li></ul><ul><li>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. </li></ul>

×