Activity 4.7.1.
What’s the minimum and maximum numbers for an int? What happens if you go beyond these limits with - 1 or + 1?
java.lang package, which is imported by default into all Java programs. The Integer class and Double class are wrapper classes that create objects from primitive types of int and double respectively. Sometimes you need to create a wrapped object for a primitive type so that you can give it to a method that is expecting an object, or to put it in a collection like an ArrayList that we will see in the next lesson. The wrapper classes are also used to convert strings to primitive data types and to convert primitive data types to strings which is useful when using the Scanner class with input.Integer and Double Objects
Integer and Double objects. In Java version 7, you can use a constructor like new Integer(2) to create an object with the value 2 in it. In Java 9 and later, you can just use Integer i = 2; to create an object with the value 2 wrapped in it. Integer and Double objects are immutable, meaning once the objects are created, their attributes (their values) cannot be changed.// in older versions of Java
Integer i = new Integer(2); // create an object with 2 in it
Double d = new Double(3.5); // create an object with 3.5 in it
// in newer versions of Java (9+)
Integer i = 2;
Double d = 3.5;
int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class or assigned to a variable of the corresponding wrapper class. Here’s an example of autoboxing.Integer i = 2;
Double d = 3.5;
Integer i = 2; // autoboxing - wrap 2
int number = i; // unboxing - back to primitive type
Integer and Double classes have methods called Integer.parseInt and Double.parseDouble that can be used to convert strings to numbers. They are often used with the Scanner class to convert input which is read in as a String into an int or double so that you can create arithmetic expressions (do math) or create logical conditions that test the values against other numbers using relational operators like < and >. These methods are listed on the Java Quick Reference Sheet provided during the AP exam.static int parseInt(String s) returns the String argument as a signed int.static double parseDouble(String s) returns the String argument as a signed double.Integer.parseInt method to convert the speed from a string to an integer so we can compare the speeds.findMaxSpeed and findAverageSpeed methods below.Integer class and Double class are wrapper classes that create objects from primitive types.Integer class and Double class are part of the java.lang package.Integer object is immutable, meaning once an Integer object is created, its attributes cannot be changed. A Double object is immutable, meaning once a Double object is created, its attributes cannot be changed.int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is:
passed as a parameter to a method that expects an object of the corresponding wrapper class assigned to a variable of the corresponding wrapper class
Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is:
passed as a parameter to a method that expects a value of the corresponding primitive type assigned to a variable of the corresponding primitive type
Integer method—including what it does and when it is used—is part of the Java Quick Reference: static int parseInt(String s) returns the String argument as a signed int.Double method—including what it does and when it is used—is part of the Java Quick Reference: static double parseDouble(String s) returns the String argument as a signed double.