26 August 2020

Reading Download j1.pdf. This discusses the primitive types in detail and the use of jshell

The file session.txt contains the entire terminal session for the class. The .out file is a translation to HTML special character entities.

JVM, JDK and JRE: What are they?

These are three important items you should know about

JDK The Java Development Kit contains all of the tools you need to create Java programmers. It contains the javac compiler.

JRE The Java Runtime Environment contains the Java libraries and the java command that allows you to run Java programs.

JVM This is a virtual computer (implemented in software). Its machine language is called Java bytecode When you run Java, the Java bytecode is converted into your native machine code, and run by your computer's CPU. Java bytecode is a standard; it is the same on on all computing platorms. The JVM varies by local machine architecture because it produced native machine code which gets run.

This six-minute video gives more detail. It's a "required reading."

Casting About: Primitive Types

A cast is a temporary request to regard an object of one type as being of another. Not all casts are allowable, because they might not make sense.

Grammatically a cast is done like this.

(newType) datum

The four integer types: byte, short, int, and long We have listed them here in increasing order of size (1 byte, 2 bytes, 4 bytes, 8 bytes). Casting from a smaller to a bigger type can be done implicitly

byte b = 5;
int x = b;

This is called "upcasting;" in this case you are moving to a bigger house, so all of your furniture and possessions will fit.

It is permissible to cast explicitly like so.

byte b = 5;
int x = (int) b;

Casting to a smaller type must be done explicitly. You need to be careful.

jshell> int whole = 5;
whole ==> 5

jshell> byte b = (byte) whole
b ==> 5

jshell> b
b ==> 5

jshell> whole = 551
whole ==> 551

jshell> byte b  = (byte) whole
b ==> 39

DAMAGE! What happened? To find out, let's look at the two numbers' binary expansions.

jshell> Integer.toString(551, 2)
$6 ==> "1000100111"

jshell> Integer.toString(39, 2)
$7 ==> "100111"

Apparently, only the last 8 bits "made the move" and rest got left at the curb. Ouch. When you downcast, you can do damage and be left.... downcast.

We now know how casting works among the four integer types. Java will rebel if you try to downcast implictly. It will demand an explicit cast.

jshell> byte c = 551
|  Error:
|  incompatible types: possible lossy conversion from int to byte
|  byte c = 551;
|           ^-^

How about boolean? Let's try it. First, just for fun, let's see what Python does.

>>> int(True)
1
>>> 

Now for Java

jshell> int foo = (int) true;
|  Error:
|  incompatible types: boolean cannot be converted to int
|  int foo = (int) true;
|                  ^--^

jshell> boolean maybe = (boolean) 1
|  Error:
|  incompatible types: int cannot be converted to boolean
|  boolean maybe = (boolean) 1;
|                            ^

The boolean is a "hermit;" it does not cast to anything else. Other types can't be cast to it.

Last time, we learned that there is fluidity between the character and integer type; this association is created by ASCII and Unicode.

jshell> (int) 'q'
$10 ==> 113

jshell> (char) 45
$11 ==> '-'

Casting from a floating-point type to an integer type causes the decimal portion of the number to be lopped off.

jshell> (int) 5.26
$12 ==> 5

jshell> (int) -5.26
$13 ==> -5

Casting from an integer type to a floating-point type is an upcast. This cast occurs in the obvious way.

jshell> double d = 4;
d ==> 4.0

jshell> long ell = 3243434;
ell ==> 3243434

jshell> (double) ell
$16 ==> 3243434.0

In Python, you can cast a numeric string to an int or a float. Try this in Java and get rebuffed.

jshell> String num = "567";
num ==> "567"

jshell> (int) num
|  Error:
|  incompatible types: java.lang.String cannot be converted to int
|  (int) num
|        ^-^

These handy toys will do the job for you.

jshell> int n = Integer.parseInt(num);
n ==> 567

jshell> n
n ==> 567

jshell> String avocado = "6.02e23"
avocado ==> "6.02e23"

jshell> double a = Double.parseDouble(avocado);
a ==> 6.02E23

Let us talk about objects

What is an object? An object is just a chunk of memory allocated for a specific purpose.

Objects have three important attributes.