Wrapper Classes: Integer and Double

Part of: Using Objects

Objects That Wrap Primitives Sometimes you need a primitive value to behave like an object : for example to store it where only objects are allowed. Java provides wrapper classes : Integer wraps an int, and Double wraps a double. Autoboxing and Unboxing Modern Java converts automatically: - Autoboxing : a primitive becomes its wrapper. Integer x = 5; quietly calls Integer.valueOf(5). - Unboxing : a wrapper becomes its primitive. int y = x; quietly calls x.intValue(). Because of this, you can usually mix int and Integer in arithmetic, and the compiler inserts the conversions for you. Useful Static Members Wrapper classes hold handy static utilities and constants: parseInt and parseDouble turn text (such as input read as a String) into numbers you can compute with. Note that these two parse methods are a platform convenience for reading input; they are not on the AP Java Quick Reference and are not tested on the exam. The tested wrapper core is autoboxing/unboxing and the Integer.MIN VALUE / Integer.MAX VALUE constants. Equality Pitfall Since wrappers are objects , == compares references, just like Strings. Use .equals() or unbox to compare values: When to Use Wrappers - Storing numb

Challenge: Sum of Parsed Numbers