Professional Documents
Culture Documents
Wrappers: Java 'S Wrapper Classes For The Primitives Types: Steve Bossie
Wrappers: Java 'S Wrapper Classes For The Primitives Types: Steve Bossie
Steve Bossie
arg # 0 = 0 arg # 1 = 42 arg # 2 = 999 arg # 3 = 0.0 arg # 4 = 1.42 arg # 5 = 9.0008
Sample values:
boolObj new Boolean(Boolean.TRUE); charObj = new Character('a'); byteObj = new Byte("100"); shortObj = new Short("32000"); intObj = new Integer(2000000); longObj = new Long(500000000000000000L); floatObj = new Float(1.42); doubleObj = new Double(1.42);
printWrapperInfo(); //method to print objects above
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);
} }
Final Notes
Javas 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.5s generics, autoboxing and the enhanced for loop to avoid unclear code.