












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













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
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 ” );
Integer i1 = Integer.valueOf(42); Boolean b1 = Boolean.valueOf( “ false ” ); System.out.println(i1.intValue()); System.out.println(b1.intValue());
=> 42 false
// 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 { } }}}
=>
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:
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); } }