wrappers ppt

21
Week 5

Upload: patel-prit

Post on 20-Dec-2015

293 views

Category:

Documents


5 download

DESCRIPTION

fvsdg

TRANSCRIPT

Week 5

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

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 (will study later in Java):

List<Integer> a = new ArrayList<Integer>();methodRequiringListOfIntegers(a);

Wrapper.valueOf() takes a value (or string) and returns an object of that class:

Integer i2 = Integer.valueOf(“42”);

Boolean b2 = Boolean .valueOf(“true”);

Long n1 = Long.valueOf(“42000000L”);

public class WrapperDemo {

public static void main(String args[]) {

Boolean bool = Boolean.valueOf("true"); Character c = new Character('c'); Byte b = Byte.valueOf("12"); Short s = Short.valueOf("2"); Integer i = Integer.valueOf("13245"); Long l = Long.valueOf("12341234"); Float f = Float.valueOf("11234.1234"); Double d =

Double.valueOf("43213241234.123412341234");

System.out.println(bool); System.out.println(c); System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); } }

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.boolValue());

=>42false

The Wrapper class for each primitive type has a method parseType() to parse a string representation & return the literal value.

Integer.parseInt(“42”) => 42Boolean.parseBoolean(“true”) => trueDouble.parseDouble(“2.71”) => 2.71//…

Common use: Parsing the arguments to a program:

public static void main(String[] args) { // TODO Auto-generated method stub

for (int i = 0; i < args.length; i++) { try { System.out.println(Integer.parseInt(args[i])); } catch (Exception e) { try { System.out.println(Float.parseFloat(args[i])); } finally { } } } } }

=>

arg # 0 = 0arg # 1 = 42arg # 2 = 999arg # 3 = 0.0arg # 4 = 1.42arg # 5 = 9.0008

Object boolObj = new Boolean(Boolean.TRUE); Object charObj = new Character('a'); Number byteObj = new Byte("100"); Number shortObj = new Short("32000"); Number intObj = new Integer(2000000); Number longObj = new Long(500000000000000000L); Number floatObj = new Float(1.42); Number doubleObj = new Double(1.42);

System.out.println(boolObj); // true System.out.println(charObj); // a ….. …………

=> For Boolean & Character Wrappers (Objects wrappers): Boolean:true Character:a

For Number wrappers: Byte:100 Short:32000 Integer:2000000 Long:500000000000000000 Float:1.42 Double:1.42

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

=> Byte:127 Short:32767 Integer:2147483647 Long:9223372036854775807 Float:3.4028235E38 Double:1.7976931348623157E308

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)

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

import java.util.Map; import java.util.TreeMap;

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

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. We continue by writing the same program with auto boxing, generics, and an enhanced for loop:

Generic implementation rewritten with auto boxing, generics, and an enhanced for loop:

public class Freq { public static void main(String args[]) { Map<String, Integer> m = new TreeMap<String, Integer>(); for (String word : args) { Integer freq = m.get(word); m.put(word, (freq == null ? 1 : freq + 1)); } System.out.println(m); }}

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.

Java’s wrapper classes are useful and provide a great deal of functionality, well beyond that of the primitive types.

Follow Wrapper Classes Study on-line to do some exercises