12 October 2020

Opportunity for Quantum Computing class - Applicants will be accepted on a rolling basis in two rounds, beginning October 9 and October 15, so the time is right to apply right now.

This opportunity is open for free to high school students and above and is provided by a combination of MIT, Oxford University and IBM's open source Qiskit open source quantum software development kit.

Article announcing the program: The announcement

Housekeeping There will be a Python lab practical next week. A practice practical will be released this week. I will announce its presence on Canvas.

Java and Python Classes

What is a JVM? Java has a VM. All computers have a machine language. Your computer only understands its machine language. JVM has a machine langugage same for all platorms. Java byte code. It takes the byte code, trananslates it into machine code and executes it.

What is a JDK? This allows you to create Java programs. javac compiles, java runs.

What is a class? It is a blueprint for the making of objects. Objects have: state, identity, behavior. A class specifies these. An object is just stuff stored in memory in some structured way.

What is compilation? Java source code is converted into Java byte code. The javac command does this.

How do I execute Java code? Yu need a main method and you use java

Let's write the simplest Java program. This does nothing.


public class Hello
{
}

You can compile it. The result is a file named Hello.class. I show a hex dump of the file here; it is not human-readable. It's Java byte code.

unix> javac Hello.java
unix> ls
Hello.class Hello.java
unix> xxd Hello.class
00000000: cafe babe 0000 003a 000d 0a00 0200 0307  .......:........
00000010: 0004 0c00 0500 0601 0010 6a61 7661 2f6c  ..........java/l
00000020: 616e 672f 4f62 6a65 6374 0100 063c 696e  ang/Object...<in
00000030: 6974 3e01 0003 2829 5607 0008 0100 0548  it>...()V......H
00000040: 656c 6c6f 0100 0443 6f64 6501 000f 4c69  ello...Code...Li
00000050: 6e65 4e75 6d62 6572 5461 626c 6501 000a  neNumberTable...
00000060: 536f 7572 6365 4669 6c65 0100 0a48 656c  SourceFile...Hel
00000070: 6c6f 2e6a 6176 6100 2100 0700 0200 0000  lo.java.!.......
00000080: 0000 0100 0100 0500 0600 0100 0900 0000  ................
00000090: 1d00 0100 0100 0000 052a b700 01b1 0000  .........*......
000000a0: 0001 000a 0000 0006 0001 0000 0001 0001  ................
000000b0: 000b 0000 0002 000c                      ........
unix> 

Look what happens when I try to run it.

unix> java Hello
Error: Main method not found in class Hello, please define the main method as:
   public static void main(String[] args)
   or a JavaFX application class must extend javafx.application.Application
unix> 

To make it executable, we add the method recommended in the error message.


public class Hello
{
    public static void main(String[] args)
    {
        System.out.println("Hello, World!");
    }
}

The method System.out.println puts stuff to stdout; it works like Python's print. We now run it.

unix> java Hello
Hello, World!
unix> 

The keywordds public and private are access specfifers. public means, "visible outside of the class," and private means , "only visible inside the class."

The class keyword idicates that we are making a class; this isa blueprint for objects produced by the JVM..

The keyword static indicates that the main method is is a class methd, not a method for an instance.

Three methods are handy for printing.

  • System.out.println puts to stdout with a newline
  • System.out.print puts to stdout, but no newline
  • System.err.print(ln) - puts to stderr, which by default is the screen.

A python program that dos the same thing looks like this.


print("Hello, World!")

Python has classes too. Let's make one that looks like our Java program


class Hello(object):
    def speak(self):
        return "Hello, World!"

def main():
    h = Hello()    #make new Hello object
    print(h.speak())
main()

Java has three types of comments which you can see here.


public class Hello
{
    //this is a one-line comment.  It si the same as Python's #
    /*
     * This is a multiline comment
     */
    /**
     * This is a javadoc comment
     */
    //  def main(): This is your main routine
    public static void main(String[] args)
    {
        System.out.println("Hello, World!");
    }

Types

Python has these types of objects.

dictionary
string
list
bool
int
float
complex

Python variables have NO type.

In contrast Java variables AND data have types. Let us begin with Java's integer types. The ++ and -- postfix operators are "primitive" in the sense that they act on integer types as raw binary numbers.

If you do arithmetic on shorts or bytes, the result is promoted automatically to an int. This does not happen with the autoincrement and autodecrement operators.

TypeDescritpion
byte This is an 8-bit (one byte) integer.
shortThis is a two-bit integer
int This is a four-bit integer
longThis is an 8-bit integer.

Using jshell Invoke this Java REPL by typing jshell in a command window.

When defining a Java variable, you must specify the variable's type.


(base) MAC:Mon Oct 12:09:57:~> jshell
|  Welcome to JShell -- Version 14.0.1
|  For an introduction type: /help intro

jshell> int x = 5;
x ==> 5

This varible is an integer variable with value 5. The type of a variable is fixed for the variables lifetime and cannot be changed. Assignment, as it is in Python, is a worker statement. Worker statements in Java must end with a semicolon or the compiler will shriek at you and abort.

OK, break the rules and receive a nasty ululation.


jshell> x = "cowabunga"
|  Error:
|  incompatible types: java.lang.String cannot be converted to int
|  x = "cowabunga"
|      ^---------^

Compound assignment is identical to Python.


jshell> x += 4;
$2 ==> 9

No surprises here.


jshell> 4*5
$3 ==> 20

jshell> 4 + 5
$4 ==> 9

jshell> 4 - 5
$5 ==> -1

Integer division here.


jshell> 4/5
$6 ==> 0

Mod works but ** does not.


jshell> 4%5
$7 ==> 4

jshell> 4**5
|  Error:
|  illegal start of expression
|  4**5
|    ^

jshell> // you got POW for using pow.

jshell> Math.pow(4,5)
$8 ==> 1024.0

I seldom take more than a 40 lb byte.


jshell> byte b = 6;
b ==> 6

jshell> while(b > 0){ b++;}

jshell> b
b ==> -128

jshell> b--;
$12 ==> -128

jshell> b
b ==> 127

A byte is an 8-bit integer stored in two's complement notation. Notice how type overflow introduces doughnutting.

Now you will get short shrift.


jshell> short s = 0;
s ==> 0

jshell> s
s ==> 0

jshell> while(s >= 0){s++;}

jshell> s
s ==> -32768

jshell> s--;
$19 ==> -32768

jshell> s
s ==> 32767

Let us now inspect int


jshell> int i = 0;
i ==> 0

jshell> while(i >= 0){i++;}

jshell> i
i ==> -2147483648

jshell> i--;
$24 ==> -2147483648

jshell> i
i ==> 2147483647

The long type will take too long to do this. So.... punt!


jshell> Long.MAX_VALUE
$27 ==> 9223372036854775807

jshell> Long.MIN_VALUE
$28 ==> -9223372036854775808

Someone asked, "What's with the capital letters?" All-caps in an identifier indicates that this identifier is a constant. This is a universally-observed convention in Java.

Here is a constant we all know and love.


jshell> Math.PI
$29 ==> 3.141592653589793

More Types

TypeDescription
float This is an four-bit IEEE 754 floating point number.
doubleThis is an 8-bit IEEE 754 floating-point number. We will use this type for floating point numbers in this class.
boolean There are two boolean constants, true and false.
charThis Java's character type. It has some fluidiity with int.

Creating a double is a simple matter


jshell> double x = 5.6;
x ==> 5.6

Casting About

This should come as no surprise.


jshell> (int) x
$31 ==> 5

jshell> (double) 4
$32 ==> 4.0

Truthiness does not manifest itself here.


jshell> (boolean) 5
|  Error:
|  incompatible types: int cannot be converted to boolean
|  (boolean) 5
|            ^

Punish-mint. Not spearmint, peppermint, or experiment. You can see the boolean constants here.


jshell> true
$33 ==> true

jshell> false
$34 ==> false

Java has the same relational operators as Python.

< <= >= > == !=

No suprise here. It's just like Python or JavaScript.


jshell> 4 < 5
$35 ==> true

Here we see Java's char type. It has some fluidity with the int type, which is quite useful.


jshell> char c = 'a'
c ==> 'a'

jshell> (int) c
$37 ==> 97

jshell> (char) 945
$38 ==> 'α'

Java also has a String type as you see here.


jshell> String foo = "bar";
foo ==> "bar"

And..... we are out of time. :(


jshell> /exit
|  Goodbye