5 April 2022

useful python video

Ordering

The numeric primitive types have the relational operators, < and friends. What about String and BigInteger?

The standard way to compare to objects for order is compareTo. Classes having this method will implement the Comparable interface. If a class implements this interface it is guaranteed to have a compareTo method.

How do I know if a class implements Comparable? Here is a screenshot of the top of the String API page.

header of the string API page

Under All Implemented Interfaces: you see Comparable<String>. This tells you that objects in this class are orderable using compareTo.

How to Order The predicate

x.compareTo(y) rel 0

returns true if you have x rel y, where rel is any of < > <=, or >=. Use equals and ! for equality and inequality.

Here we use relational operators on primitives.


jshell> 5  < 6
$1 ==> true

jshell> 7*7 == 49
$2 ==> true

jshell> 5.6 < 2.2
$3 ==> false

Here comes the error train.... SPLAT!



jshell> String a = "hello"
a ==> "hello"

jshell> String b = "goodbye"
b ==> "goodbye"

jshell> a < b
|  Error:
|  bad operand types for binary operator '<'
|    first type:  java.lang.String
|    second type: java.lang.String
|  a < b
|  ^---^

No, do this.


jshell> a.compareTo(b) < 0
$6 ==> false

jshell> b.compareTo(a) < 0
$7 ==> true

This works for BigIntegers too.


jshell> BigInteger x = BigInteger.valueOf(34);
x ==> 34

jshell> BigInteger y = new BigInteger("45");
y ==> 45

jshell> x.compareTo(y) < 0
$10 ==> true

jshell> x.compareTo(y) <= 0
$11 ==> true

jshell> x.equals(y)
$12 ==> false

jshell> !x.equals(y)
$13 ==> true

Lagniappe The String class has the method compareToIgnoreCase which works like compareTo but it ignores the case of alpha characters.

Java 17 switch

You might like this nifty new switch statement.


public class Animal
{
    public static void main(String[] args)
    {
        String letter = args[0];
        switch(letter)
        {
            case "A", "a" -> System.out.println("alligator");
            case "B", "b" -> System.out.println("bobcat");
            case "C", "c" -> System.out.println("carical");
            case "D", "d" -> System.out.println("dromedary");
            default -> System.out.println("No such annie-mule");
        }
    }
}

Here is the old way. Try removing the ugly break statements and you will quickly see why they are ... necessary.

You are going to see this lots in OPC.


public class Animal
{
    public static void main(String[] args)
    {
        String letter = args[0];
        switch(letter)
        {
            case "A", "a" -> System.out.println("alligator");
            case "B", "b" -> System.out.println("bobcat");
            case "C", "c" -> System.out.println("carical");
            case "D", "d" -> System.out.println("dromedary");
            default -> System.out.println("No such annie-mule");
        }
        old(letter);
    }
    public static void old(String letter)
    {
        switch(letter)
        {
            case "A", "a":
                System.out.println("alligator");
                break;  //puts you out of the switch statement
            case "B", "b":
                System.out.println("bobcat");
                break;
            case "C", "c":
                System.out.println("carical");
                break;
            case "D", "d":
                System.out.println("dromedary");
                break;
            default:
                System.out.println("No such annie-mule");
        }
    }
}

Switch can also be a statement. Check this out.


public class Animal
{
    public static void main(String[] args)
    {
        String letter = args[0];
        switch(letter)
        {
            case "A", "a" -> System.out.println("alligator");
            case "B", "b" -> System.out.println("bobcat");
            case "C", "c" -> System.out.println("carical");
            case "D", "d" -> System.out.println("dromedary");
            default -> System.out.println("No such annie-mule");
        }
        old(letter);
        System.out.println(statement(letter));
    }
    public static void old(String letter)
    {
        switch(letter)
        {
            case "A", "a":
                System.out.println("alligator");
                break;  //puts you out of the switch statement
            case "B", "b":
                System.out.println("bobcat");
                break;
            case "C", "c":
                System.out.println("carical");
                break;
            case "D", "d":
                System.out.println("dromedary");
                break;
            default:
                System.out.println("No such annie-mule");
        }
    }
    public static String statement(String letter)
    {
        String foo = switch(letter)
        {
            //this statement must have a default, or it will not compile!
            case "A", "a" -> "alligator";
            case "B", "b" -> "bobcat";
            case "C", "c" -> "carical";
            case "D", "d" -> "dromedary";
            default -> "No such annie-mule";
        };
        return foo;
    }
}

Check this cool thing out.


public class Experiment
{
    public static void main(String[] args)
    {
        int foo = switch(args[0])
        {
            case "a", "b", "c" ->
            {
                System.out.println("start of alphabet");
                yield 1;
            }
            case "x", "y", "z" ->
            {
                System.out.println("end of alphabet");
                yield 2;
            }
            default ->
            {
                System.out.println("middle of alphabet");
                yield 3;
            }
        };
       System.out.printf("%s was yielded.\n", foo);
    }
}