22 March 2022

So Far

We have seen some interesting differences between Java and Python

Python Java
Variables are typeless names. The type of a variable is an immutable property of that variable
Python makes all decisions about types at run time. Python is "duck typed." All type information must be known at compile time. Java is a statically compiled language.
Python programs run until there is an error or until they run out of code. Java programs only run after they are compiled. Python has no compilation step. If a Java program has a compilation error, compilation aborts and no bytecode is generated.
Classes are an optional feature of Python. All Java code must be placed inside of a class

What is the JVM? Every chip has a machine language, which consists of sequences of bytes. The compilation process generates machine code for your computer, which then executes it.

A virtual machine is a computer implemented in softweare. The JVM is one of these. Compilation of Java generates Java bytecode, which is the machine language of the JVM.

Your bytecode is translated into your machine's machine code by the Java runtime environment. Java bytecode is the same on all platforms. What differs is the Java runtime environment, which translates Java bytecode into machine code for each individual platform.

Code we write in this class is platform-invariant.

Are you my type?

Introducing jshell This is Java's REPL; it resembles Python's REPL (interactive mode). Your jshell session is in your block folder.

This is great. JShell Tutorial

Primitive Types Java has eight primitive types. This nifty table summarizes them. The largest of these data types is 8 bytes (64 bits).

FamilyTypeDescription
integer typesbyteThis is a one-byte integer.
shortThis is a two-byte integer.
intThis is a four-byte integer.
longThis is a eight-byte integer.
floating-point typesfloatThis is a four-byte floating-point number.
doubleThis is a eight-byte floating-point number.
boolean typebooleanThis holds a true/false value.
character typecharThis holds single Unicode character.

Variables of primitive type point directly at their values. They do not store a memory address as they do in Python. So, the values of primitive types are stored right on the stack with their variables.

Variables of integer types are stored in two's complement notation. They can experience type overflow. The videos linked here are an excellent investment of time and will give you insight into why type overflow can occur.

Object Types We have met one object type, String. Just as in Python, strings are immutable character sequences.

Variables of object type store the memory addresss (an integer) of their objects, which live on the heap. So no piece of data larger than 8 bytes is stored on the stack.