Yesterday's Language Features
Both Languages
class
This is a blueprint for the creation of objects. In it, you specify state and behavior.- instance: Objects created using a class are called instances of the class.
- method: this is function attached to an object. All functions in Java are methods. Methods can depend on object state.
Java
- constructor: this it the OBGYN of Java objects
created by class. An imporant postcondtion is for all
state to be explicitly initialized. It runs every time
a new object is creatd by a call to
new
. The constructor must have the same name as the class, and it is the only method with no return type. new
Use this keyword when creating a new object. It causes the constructor of the class to be called.- tacit call to
new
: This occurs when making Strings. It is idomatic in Java not to usenew
when creating a stirng. 99.44% of the time you will do this.String s = "fooment";
- signature: The signature of a method is the list of types in its parameter list. The parameter names are not a part of the sig.
- method name overloading: Several methods can have the same name if they have different sigs. Use this only for closely-related methods.
void
Use this keyword as a return type when a method has no return value. It is an inheritance from the C language.public
When used on a class, this says the class is visible outside of its file. When used on a method, it says the method is visible outside of the class.private
When used on a method or a state variable, , this says the item is not visible outside of its file.void
Use this keyword as a return type when a method has no return value. It is an inheritance from the C language.public String toString()
This method allows you to create a string representation of your object.public boolean equals(Object o)
This method allows you to decide when your object is "equal" to another object. Note that objects of different types should never be declared equal. This is the species test.- package: This is a collection of related classes.
- module: This is a collection of related packages.
Object
This is the root class in the Java class hierarchy. All classes, including user-defined ones, come equipped with all of theObject
methods.
Python
- dunder method: This is a method in a Python class that has a special puropose.
__init__
This method is called when a new instance of a class is created. It acts in a manner similar to a constructor in Java.__eq__
This method is called when == is used to compare two objects.__str__
This method gives a string representation of an object. It is automatically called on an object by theprint
function.
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 objectnull
.
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.