Use the following problems to test your object, immutability, and switch expression programming prowess. I strongly encourage you to give each problem a try before you turn to the solutions and download the example programs:
- Checking null references in functional style and imperative code: Write a program that performs the null checks on the given references in a functional style and imperative code.
- Checking null references and throwing a customized NullPointerException error: Write a program that performs the null checks on the given references and throws NullPointerException with custom messages.
- Checking null references and throwing the specified exception (example, IllegalArgumentException): Write a program that performs the null checks on the given references and throws the specified exception.
- Checking null references and returning non-null default references: Write a program that performs the null checks on the given reference, and if it is non-null, then return it; otherwise, return a non-null default reference.
- Checking the index in the range from 0 to length: Write a program that checks whether the given index is between 0 (inclusive) and the given length (exclusive). If the given index is out of the [0, given length] range, then throw IndexOutOfBoundsException.
- Checking the subrange in the range from 0 to length: Write a program that checks whether the given subrange [given start, given end] is within the bounds of the range from [0, given length]. If the given subrange is not in the [0, given length] range, then throw IndexOutOfBoundsException.
- equals() and hashCode(): Explain and exemplify how equals() and hashCode() methods work in Java.
- Immutable objects in a nutshell: Explain and exemplify what is an immutable object in Java.
- Immutable string: Explain why the String class is immutable.
- Writing an immutable class: Write a program that represents an immutable class.
- Passing/returning mutable objects to/from an immutable class: Write a program that passes and returns a mutable object to/from an immutable class.
- Writing an immutable class via the Builder pattern: Write a program that represents an implementation of the Builder pattern in an immutable class.
- Avoiding bad data in immutable objects: Write a program that prevents bad data in immutable objects.
- Cloning objects: Write a program that exemplifies shallow and deep cloning techniques.
- Overriding toString(): Explain and exemplify practices for overriding toString().
- switch expressions: Provide a brief overview of the switch expressions in JDK 12.
- Multiple case labels: Write a snippet of code for exemplifying the JDK 12 switch with multiple case labels.
- Statement blocks: Write a snippet of code for exemplifying the JDK 12 switch with case labels that point to a curly-braced block.