7 April 2022

Last Time We learned about two things, ordering and switch statements.

When is something orderable?

The switch statement examples are displayed on yesterday's page. This statement provides a convenient means for dealing with conditional logic.

You can see a side-by-side comparison of the old and new switch statements. You will see the old one in OPC. You should greatly prefer the new one.

Wrapper Classes

Every primitive type has a "wrapper" type that wraps the primitive inside of an object. Objects of these types are immutable. One benefit is that this will allow you to have collections of primitives.

stack of delicious pancakes

PrimitiveWrapper
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booeanBoolean

Useful Resource W3Schools Wrapper Class Page

Autoboxing No, we are not punching a car in the nose. Autoboxing causes primitive tpes to be automatically upgraded to objects when assigned to a variable of wrapper type.


jshell> Integer i = 5;
i ==> 5

jshell> Double d = 3.44;
d ==> 3.44

jshell> d
d ==> 3.44

Notice that i is an object.


jshell> i.getClass()
$4 ==> class java.lang.Integer

You can't dot a nekked primitive.


jshell> int k = 5;
k ==> 5

jshell> k.getClass()
|  Error:
|  int cannot be dereferenced
|  k.getClass()
|  ^--------^

Autounboxing When an object of wrapper type is assigned to a variable of primitive type, it is automatically "unboxed."


jshell> Double d = 3.44;
d ==> 3.44

jshell> double primitive = d;
primitive ==> 3.44

You an make collections of wrapper type, but not of primitive type


jshell> ArrayList<int> nums = new ArrayList<>();
|  Error:
|  unexpected type
|    required: reference
|    found:    int
|  ArrayList<int> nums = new ArrayList<>();
|            ^-^

jshell> ArrayList<Integer> nums = new ArrayList<>();
nums ==> []

jshell> nums.add(42);
$8 ==> true

jshell> nums
nums ==> [42]

jshell> nums.add(44);
$10 ==> true

jshell> nums.add(10);
$11 ==> true

jshell> nums
nums ==> [42, 44, 10]

Here is a boolean array list.


jshell> ArrayList<Boolean> foo = new ArrayList<>();
foo ==> []

jshell> foo.add(false);
$14 ==> true

jshell> foo.add(true);
$15 ==> true

jshell> foo.add(17);
|  Error:
|  incompatible types: int cannot be converted to java.lang.Boolean
|  foo.add(17);
|          ^^

Java has a set type.


jshell> TreeSet<Integer> ts = new TreeSet<>();
ts ==> []

jshell> ts.add(5);
$17 ==> true

jshell> 

jshell> ts.add(5)
$18 ==> false

jshell> ts.add(-1)
$19 ==> true

jshell> ts.add(45);
$20 ==> true

jshell> ts
ts ==> [-1, 5, 45]

jshell> ts.add(88)
$22 ==> true

jshell> ts
ts ==> [-1, 5, 45, 88]

Why are these constructors deprecated? Autoboxing and autounboxing render them obsolete.

A deep dive into Integer This class has an abundance of useful stuff.


jshell> Integer q = 5;
q ==> 5

jshell> Integer r = 6;
r ==> 6

jshell> q < r
$33 ==> true

You can do these but it's not really necessary.


jshell> q.compareTo(r) < 0
$34 ==> true

jshell> Integer.compare(5,6)
$35 ==> -1

jshell> Integer.compare(5,6) < 0
$36 ==> true

Java is smart about these prefixes.


jshell> 0b11010110101
$41 ==> 1717

jshell> 0xFF
$42 ==> 255

and dumb about this one


jshell> 0o66
|  Error:
|  ';' expected
|  0o66
|   ^

Just do this; prepend with a 0.


jshell> 066
$43 ==> 54

With parsint you have all bases covered.


jshell> Integer.parseInt("31434")
$46 ==> 31434

jshell> Integer.parseInt("abcdef", 16)
$47 ==> 11259375

jshell> Integer.parseInt("1011011110", 2)
$48 ==> 734

jshell> Integer.parseInt("202221112", 3)
$49 ==> 15269

Type.parseType(string) -> object of type type


jshell> Double.parseDouble(quack)
$51 ==> 4.2356

jshell> Double.parseDouble("6.20e23")
$52 ==> 6.2E23

jshell> Boolean.parseBoolean("false")
$53 ==> false

Here is the signum function.


jshell> Integer.signum(5)
$54 ==> 1

jshell> Integer.signum(100)
$55 ==> 1

jshell> Integer.signum(1000)
$56 ==> 1

jshell> Integer.signum(0)
$57 ==> 0

jshell> Integer.signum(-42)
$58 ==> -1

You can get a hex string.


jshell> Integer.toHexString(100)
$59 ==> "64"

jshell> Integer.toHexString(1023)
$60 ==> "3ff"

jshell> Integer.toHexString(1023).toUpperCase()
$61 ==> "3FF"

These both work.


jshell> Integer.toString(2314431)
$62 ==> "2314431"

jshell> "" + 2314431
$63 ==> "2314431"

jshell> Boolean.toString(false)
$74 ==> "false"

jshell> "" + false
$75 ==> "false"

You can get any base string.


jshell> Integer.toString(2314431, 16)
$64 ==> "2350bf"

jshell> Integer.toString(2314431, 36)
$65 ==> "1dltr"

All numeric types have max and min values.


jshell> Integer.MAX_VALUE
$66 ==> 2147483647

jshell> Long.MAX_VALUE
$67 ==> 9223372036854775807

jshell> Float.MAX_VALUE
$68 ==> 3.4028235E38

jshell> Double.MAX_VALUE
$69 ==> 1.7976931348623157E308

Size up some primitive types.


jshell> Integer.SIZE
$70 ==> 32

jshell> Short.SIZE
$71 ==> 16

jshell> Double.SIZE
$72 ==> 64

A deep dive into Character Are you upper-case? Are you numeric? Are you printable?

Characters are two bytes wide.


jshell> Character.BYTES
$70 ==> 2

Here are some useful tests of character type.


jshell> Character.isAlphabetic('a')
$71 ==> true

jshell> Character.isAlphabetic('@')
$72 ==> false

jshell> Character.isDigit('3')
$73 ==> true

jshell> Character.isDigit('a')
$74 ==> false

jshell> Character.isDigit('9')
$75 ==> true

jshell> Character.isISOControl('\n')
$76 ==> true

jshell> Character.isISOControl('a')
$77 ==> false

jshell> Character.isISOControl('\r')
$78 ==> true

jshell> Character.isWhitespace(' ')
$79 ==> true

Don't do this! Joshua Bloch has a useful little article on this. It's excerpted from his book Effective Java.


Integer total;
for(Integer i = 0; i < 100000; i++)
{
    total += i;
}

You take a performance hit becuase 100,000 boxings and unboxings occur. Use primitives as loop variables

Useful Static Methods

Every wrapper has a static method public Type parseType(String s) that turns a string into that type.

Every wrapper has a static method public String toString(Type t) that turns a primitive into an appropriate string representation.