8 October 2021

Yesterday's Language Features

Both Languages

Java

Python

Arrays and Command-Line arguments

Command-line arguments are baked right into Java. Let us run this program.


public class Commando
{
    public static void main(String[] args)
    {
        for(int k = 0; k < args.length; k++)
        {
            System.out.printf("args[%s] = %s\n", k, args[k]);
        }

    }
}

Now we run it in a command window.

$ java Commando first second third fourth
args[0] = first
args[1] = second
args[2] = third
args[3] = fourth

The type String[] means, "an array of strings.

What is an array? An array is a homogeneous fixed-sized data structure whose items are stored adjacently in memory. Item access can be done with the [] operator. Array entries are lvalues, just like Python list entries.

To make an array, do this.


int[] nums = new nums[10];
String[] names = new String[10];

Arrays are stored on the heap. For primitive types a "zeroish" value is places in each entry by the Java ovipositor.

Arrays of object type are initialized with the graveyard object null.

Let's open jshell. Here are arrays of primitive type.


jshell> int[] arr = new int[10];
arr ==> int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

jshell> boolean[] arr = new boolean[10];
arr ==> boolean[10] { false, false, false, false, false,  ... lse, false, false, false }

jshell> String[] foo = new String[10];
foo ==> String[10] { null, null, null, null, null, null, null, null, null, null }

Beware of the IndexOutOfBoundsException


jshell> arr[10]
|  Exception java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
|        at (#4:1)

Arrays are mutable and entries are lvalues.


jshell> arr[0]
$5 ==> false

jshell> arr[0] = true
$6 ==> true

jshell> arr
arr ==> boolean[10] { true, false, false, false, false, false, false, false, false, false }

Arrays are a fixed size HOMOgeneous sequence type. Indexing is done by [].


jshell> foo[0].length()
|  Exception java.lang.NullPointer: Cannot invoke "String.length()" because "REPL.$JShell$12.foo[0]" is null
|        at (#8:1)

Don't try to calculate the length of a null. You get death for your efforts. Here we populate the array with strings.


jshell> for(int k = 0; k < foo.length; k++){foo[k] = Integer.toString(k*k*k);}

jshell> foo
foo ==> String[10] { "0", "1", "8", "27", "64", "125", "216", "343", "512", "729" }

An array's toString method is pure crap.


jshell> System.out.println(foo);
[Ljava.lang.String;@7dc5e7b4

Use this instead. It's far better.


jshell> Arrays.toString(foo)
$12 ==> "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]"

jshell> foo[0] = "tapir";
$13 ==> "tapir"

jshell> Arrays.toString(foo)
$14 ==> "[tapir, 1, 8, 27, 64, 125, 216, 343, 512, 729]"

The Arrays class is class-strated. You cannot make instance of it.


jshell> Arrays dead = new Arrays();
|  Error:
|  Arrays() has private access in java.util.Arrays
|  Arrays dead = new Arrays();
|                ^----------^

Math, too.


jshell> Math doomed = new Math()
|  Error:
|  Math() has private access in java.lang.Math
|  Math doomed = new Math();
|                ^--------^

Here is an example of how to use Arrays.toString in a program.