Jump to topic 1.1 Introduction to Algorithms, Programming, and Compilers 1.2 Variables and Data Types 1.3 Expressions and Output 1.4 Assignment Statements and Input 1.5 Casting and Range of Variables 1.6 Compound Assignment Operators 1.7 Application Program Interface (API) and Libraries 1.8 Documentation with Comments 1.9 Method Signatures 1.10 Calling Class Methods 1.11 Math Class 1.12 Objects: Instances of Classes 1.13 Object Creation and Storage (Instantiation) 1.14 Calling Instance Methods 1.15 String Manipulation 2.1 Algorithms with Selection and Repetition 2.2 Boolean Expressions 2.3 if Statements 2.4 Nested if Statements 2.5 Compound Boolean Expressions 2.6 Comparing Boolean Expressions 2.7 while Loops 2.8 for Loops 2.9 Implementing Selection and Iteration Algorithms 2.10 Implementing String Algorithms 2.11 Nested Iteration 2.12 Informal Run-Time Analysis 3.1 Abstraction and Program Design 3.2 Impact of Program Design 3.3 Anatomy of a Class 3.4 Constructors 3.5 Methods: How to Write Them 3.6 Methods: Passing and Returning References of an Object 3.7 Class Variables and Methods 3.8 Scope and Access 3.9 this Keyword 4.1 Ethical and Social Issues Around Data Collection 4.2 Introduction to Using Data Sets 4.3 Array Creation and Access 4.4 Array Traversals 4.5 Implementing Array Algorithms 4.6 Using Text Files 4.7 Wrapper Classes 4.8 ArrayList Methods 4.9 ArrayList Traversals 4.10 Implementing ArrayList Algorithms 4.11 2D Array Creation and Access 4.12 2D Array Traversals 4.13 Implementing 2D Array Algorithms 4.14 Searching Algorithms 4.15 Sorting Algorithms 4.16 Recursion 4.17 Recursive Searching and Sorting Unit 4 · Data Collections · Topic 4.7 Wrapper Classes Skill 2.B
Big idea: Integer and Double wrap a primitive inside an object so it can go where only objects are allowed, such as an ArrayList. Java converts back and forth for you.
Learning objectives Develop code to use Integer and Double objects from their primitive counterparts and determine the result of using these objects. Autoboxing and unboxing Autoboxing : Java turns an int into an Integer (or a double into a Double) when an object is needed. Unboxing is the reverse.
Code Integer boxed = 42 ; // autobox
int plain = boxed + 8 ; // unbox, then add
ArrayList <Integer > list = new ArrayList <Integer >();
list.add(7 ); // autobox 7
int first = list.get(0 ); // unbox
System .out.println(plain + " " + first);Output 50 7 Parsing strings Text from a file or split comes in as Strings. Convert with the static parse methods.
Code int n = Integer .parseInt("125" );
double d = Double .parseDouble("2.5" );
System .out.println(n + 1 );
System .out.println(d * 2 );
System .out.println("125" + 1 );
System .out.println(Integer .MAX_VALUE );Output 126
5.0
1251
2147483647 Key points Integer and Double are in java.lang and are immutable.Autoboxing : primitive → wrapper automatically. Unboxing : wrapper → primitive.Integer.parseInt(s) and Double.parseDouble(s) convert Strings to numbers.Integer.MIN_VALUE and Integer.MAX_VALUE are the int limits.Watch out ArrayList can't hold primitives: write ArrayList<Integer>, not ArrayList<int>. Quick check What is Integer.parseInt("40") + Integer.parseInt("2")? Show answer 42 (int addition, not concatenation).