Download presentation
Presentation is loading. Please wait.
Published byCurtis Summers Modified over 8 years ago
1
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean
2
Wrapper Classes For example: Integer age = new Integer(40); Wrapper classes contain static methods to convert between types. For example, to convert a String to an int : int num = Integer.parseInt(str); Wrapper classes also contain constants such as MIN_VALUE and MAX_VALUE in the Integer class, which hold the smallest and largest int values Copyright © 2012 Pearson Education, Inc.
3
Wrapping: Converting from primitive to wrapper Use the wrapper class constructor to instantiate a new object. Example Character characterVar; Integer myInteger; char myChar = ‘z’; characterVar = new Character( myChar ); myInteger = new Integer( 1234 ); Unwrapping: Converting from wrapper to primitive int myInt; char myChar = ‘z’; myChar = characterVar.charValue(); myInt = myInteger.intValue(); Example Apply the conversion method to the wrapper object.
4
A SimpleList Problem. Solution SimpleList list = new SimpleList(); list.insert( new Double(53.1) ); Example SimpleList list = new SimpleList(); list.insert( 53.1 ); ^ Incompatible type for method. Can’t convert double to java.lang.Object
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.