Docsity

Java Wrapper Classes: Understanding Primitives and Objects, Slides of Java Programming

Partial preview of the text

Download Java Wrapper Classes: Understanding Primitives and Objects and more Slides Java Programming in PDF only on Docsity!

Wrappers:

Java’s Wrapper Classes for the Primitives

Types

Primitives & Wrappers

• Java has a wrapper class for each of the

eight primitive data types:

Primitive

Type

Wrapper Class

Primitive Type

Wrapper Class

boolean Boolean float Float

byte Byte int Integer

char Character long Long

double Double short Short

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 ” );

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

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 { } }}}

Parsing argument lists

=>

arg # 0 = 0

arg # 1 = 42

arg # 2 = 999

arg # 3 = 0.

arg # 4 = 1.

arg # 5 = 9.

Sample values (output from

previous slide):

=> For Boolean & Character Wrappers: Boolean:true Character:a

For Number wrappers: Byte: Short: Integer: Long: Float:1. Double:1.

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:");

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)

Double & Float: Utilities for

Arithmetic Operations:

  • Constants POSITIVE_INFINITY & NEGATIVE_INFINITY
  • Constant NaN = Not-a-Number (NaN) value.
  • Methods isNaN(), isInfinite()

Bloch’s counting program: Java 1.

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); } }

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:

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, 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.