5 October 2021

Hello, Java

Python Java
  1. You write source.
  2. You save it to disk.
  3. You run Python on it.
  4. Your code begins execution and dies if an error occurs.
  5. If no error occurs, your program has run successfully.
  1. You write source.
  2. You save it to disk.
  3. You compile. If the program does not make syntactic sense, no code generation occurs and there is nothing to execute. Errors occuring here are compile time errors.
  4. If your code compiles, you get a file containing executable code. You are not out of the woods. If you do something dumb, such as accessing a nonexistent index in a string, you get an exception, or run-time error.
  5. Barring any run-time error, your code successfully executes.

Guess what. You are still not out of the woods. Your program might make syntactic sense and compile and it might even run without error. But the semantics could be wrong and it could produce an undesired erroneous result.

What is Compiling? Installed on your lappy is a JDK; this is a Java Developer Kit. Part of this is the JRE, the Java Runtime Environment. The JRE contains the JVM, or Java Virtual Machine. As you know a virtual machine is a computer that is implemented in software.

Every computer has a machine language. The machine language of the JVM is called Java Byte Code. That is what is in the .class file that is produced.

Your First Program Make this program.


public class Empty
{
}

Now compile like so.

$ javac Empty.java

List your file and you will see a file named Empty.class. Here is a hex dump, done on a mac.

$ xxd Empty.class
00000000: cafe babe 0000 003b 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 0545  it>...()V......E
00000040: 6d70 7479 0100 0443 6f64 6501 000f 4c69  mpty...Code...Li
00000050: 6e65 4e75 6d62 6572 5461 626c 6501 000a  neNumberTable...
00000060: 536f 7572 6365 4669 6c65 0100 0a45 6d70  SourceFile...Emp
00000070: 7479 2e6a 6176 6100 2100 0700 0200 0000  ty.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                      ........

Most of it is not human readable.

You will note some things. Code exists inside a container called a class. The name of the class must match the name of the file or you will be flailed by the compiler.

Making a File Executable Recall that we make a distinction between methods and functions. Methods are functions that can depend on the state of an object. Regular function have no such attachmennt. In Java, if you want your program to execute you need a specal method named main.


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

Compile and run.

$ javac Hello.java
$ java Hello
Hello, Java!

Java's 8 Primitive Types

You must declare type when a variable is created in Java. Note the error.

To make a variable


type identifier = value;

The rules for variable names are the same as Python JS and C.


jshell> x = 5;
|  Error:
|  cannot find symbol
|    symbol:   variable x
|  x = 5;
|  ^

Do this.


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

byte one-byte signed integer


jshell> byte b = 1
b ==> 1

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

jshell> b
b ==> -128

jshell> b--
$10 ==> -128

jshell> b
b ==> 127

short two-byte signed integer


jshell> short s = 1
s ==> 1

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

jshell> s
s ==> -32768

int four-byte signed integer. Notice how it doughnuts when type overflow occurs.


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

jshell> x
x ==> -2147483648

long eight-byte signed integer


jshell> for(int k = 0; k < 60; k++){l *=2;}

jshell> l
l ==> 1152921504606846976

jshell> l *= 2
$18 ==> 2305843009213693952

jshell> l *= 2
$19 ==> 4611686018427387904

jshell> l *= 2
$20 ==> -9223372036854775808

float four-byte IEEE 754. We will likely never use this type.


jshell> float f = 1;
f ==> 1.0

double eight-byte IEEE 754. This is the same as Python's float.


jshell> double d = 1
d ==> 1.0

boolean true/false. The relational operators are just like those in Python.


jshell> 5 < 6
$23 ==> true

jshell> 5 <= 6
$24 ==> true

jshell> 5 < 6.0
$25 ==> true

char unicode character. Casting to an integer yields the unicode value for the character, and vice versa.


jshell> char quack = 'a';
quack ==> 'a'

jshell> (int) quack
$28 ==> 97

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

jshell> (char) 946
$30 ==> 'β'

jshell> (char) 947
$31 ==> 'γ'

jshell> (char) 948
$32 ==> 'δ'

jshell> (char) 5280
$33 ==> 'ᒠ'

public class Stupid
{
    public static void main(String[] args)
    {
        String s = "abcd";
        System.out.println(s.charAt(7));
    }
}