Boolean Ipsa Loquitor
I saw a lot of this in your first program.
public boolean someMethod() { if(predicate) return true; else return false; }
This is far simpler.
public boolean someMethod() { return predicate; }
Moral Let the boolean speak for itself.
Looping in the Exercises Unnecessary. Some of you were re-creating wheels instead of using them.
Looping and Not
Printing a List: Let Me Count the Ways Any of the methods shown here will print out the a list for you.
import java.util.ArrayList; public class ListPrinting { public static void main(String[] args) { ArrayList<String> zoo = new ArrayList<>(); zoo.add("antelope"); zoo.add("badger"); zoo.add("carical"); zoo.add("dingo"); zoo.add("elephant"); zoo.add("fox"); sizeMe(zoo); }
Let's discuss each in succession.
public static void printWhile(ArrayList<String> zoo) { int index = 0; while(index < zoo.size()) { System.out.println(zoo.get(index)); index++; } }
This is a typical use of the while
loop. Really
this loop is an unfitting tool here. It is prone to error because
if you forget to increment in the loop's block, you will get spewage.
public static void printFor(ArrayList<String> zoo) { //This is the C for loop //for(init; test; between){} for(int index = 0; index < zoo.size(); index++) { System.out.println(zoo.get(index)); } }
This is the classic C-style loop. The header has the form
for(init; test;between) { //block }
It is, fundamentally, a modified while
. Recall that
init
runs once when the loop is encountered and
never again. Then test
runs; if it evaluates to
false
, the loop ends. Otherwise, the block runs,
between
runs, and the cycle repeats.
It is less error-prone than the while
loop.
public static void printForCollection(ArrayList<String> zoo) { //This is the C for loop //collections for loop for(String item: zoo) { System.out.println(item); } }
This is the collections for
loop. Admire its succinctness
and the ease with which it is understood.
public static void printForEach(ArrayList<String> zoo) { zoo.forEach(System.out::println); }
Whoa! What is happening here? A lot. First of all,
we are passing in a method reference; this treats System.out.println
as a function object. What kind of function. Go into the ArrayList docs and look
up forEach
. You will see it is a Consumer<? super E>
,
where E
is the type parameter of the array list. A consumer is a function
taking a single argument of type E
or any supertype thereof, and which
has a void
return value.
public static void sizeMe(ArrayList<String> zoo) { zoo.forEach( str -> System.out.println(str.length())); } }
Here we are using a lambda, an anonyomous function as our consumer.
Arrays
An array in Java is a fixed-sized mutable sequence type. Here we
create an array. Notice that its toString method stinks. However, the
static service class java.util.Arrays
comes riding to the rescue.
/************************************************** * Author: Morrison * Date created: 8 Sep 2020 * Date last modified: 8 Sep 2020 **************************************************/ import java.util.Arrays; public class ArrayExample { public ArrayExample() { } public static void main(String[] args) { int[] x = new int[6]; System.out.println(x); System.out.println(Arrays.toString(x)); for(int item: x) { System.out.println(item); } } }
The Java ovipositor initializes array entries with these values
type | value |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
char | the null character whose byte value is 0 |
float | 0.0 |
double | 0.0 |
boolean | false |
Object | null |
Here we see boolean and character arrays getting initialized.
jshell> boolean[] foo = new boolean[5]; foo ==> boolean[5] { false, false, false, false, false } jshell> char[] goo = new char[5] goo ==> char[5] { '\000', '\000', '\000', '\000', '\000' }
Here is a string array.
jshell> String[] s = new String[5]; s ==> String[5] { null, null, null, null, null } jshell> s s ==> String[5] { null, null, null, null, null } jshell> s[0] $6 ==> null
Don't call methods on null objects!
jshell> s[0].length() | Exception java.lang.NullPointerException | at (#7:1)
Observe that length is a property, not a method.
jshell> s.length $8 ==> 5
This is taboo.
jshell> s.length = 7; | Error: | cannot assign a value to final variable length | s.length = 7; | ^------^