Last Time
- We learned what
String[] args
is: it is an array of strings that holds command-line arguments entered by the executor of the program. - We learned that
static
items are properties of the class as a whole. Items that arae not static are calledinstance items
. - The rule of the greased wall says that instance items have access to static items, but static items do not have naked access to instance items.
- Java has static service classes such as
Math
andArrays
which are just containers that hold useful static methods. - Every primitive type has a corresponding wrapper type that is an object. Wrapper classes include useful static methods that do useful stuff with the primitive they wrap.
- You can assign directly to wrapper types. This code
is legit.
Integer x = 4; //autoboxing int z = x; //autounboxing
Other Wrapper Types
We will explore the rest of the wrapper types using jshell. Here is a decoder ring.
Primitive | Wrapper |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
Here are common featurs to all wrapper classes.
- All of these create immutable objects.
- All have a static
toString
method. - All reside in package
java.lang
so no import statement is necessary. - All numeric types have static
MAX
andMIN
static constants. - All numeric types have static
SIZE
andBYTES
static constants. - All have a parse method to cast a string to the primitive type.
Here is a use case for one of the parse methods.
import java.util.Scanner;
public class Hoover
{
public static void main(String[] args)
{
Scanner vacuum = new Scanner(System.in);
System.out.print("Enter a integer: ");
String foo = vacuum.next();
int x = Integer.parseInt(foo);
System.out.printf("The square of %s is %s.\n", x, x*x);
}
}