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.
- state: This is what an object KNOWS.
- identity: This is an object's physical presence in memory. This is what an object IS
- behavior: This is embodied in an object's methods. This is what an object DOES.
I'm makin' it and checkin' it twice
Yeah.... lists. Python has 'em. So does Java.
Java has an object called an ArrayList
This is
a generic type; you must specify the type of the
enteries in the list in a type parameter.
jshell> ArrayList<String> al = new ArrayList<>(); al ==> []
We now have an empty list of strings. Notice the use of the
keyword new
; this tells the JVM to make a new object on
the heap. The variable al
stores the memory address
of that object and "know how to find it" on the heap.
Let us place some items on the list.
`jshell> al.add("oink"); $24 ==> true jshell> al al ==> [oink] jshell> al.add("moo") $26 ==> true jshell> al.add("baaaa"); $27 ==> true jshell> al.add("blahooo") $28 ==> true jshell> al al ==> [oink, moo, baaaa, blahooo]
The add
method returns a true
to indicate
it has successfully placed each item on the list.
Let's try to access an item a la Python
jshell> al[0] | Error: | array required, but java.util.ArrayList<java.lang.String> found | al[0] | ^---^
Uh oh, crash and burn. We get items this way.
jshell> al.get(0) $30 ==> "oink" jshell> al.get(2) $31 ==> "baaaa"
Here is how to change "oink" to "snuffle". You need to send two pices of information: which index you want the change at, and what you want it changed to.
jshell> al.set(0, "snuffle"); $33 ==> "oink" jshell> al al ==> [snuffle, moo, baaaa, blahooo]
One thing you should notice is that set
returns
whatever you are evicting. Check this out.
jshell> String evictee = al.set(0, "oink"); evictee ==> "snuffle" jshell> al al ==> [oink, moo, baaaa, blahooo] jshell> evictee evictee ==> "snuffle"
Go ahead and quit jshell
and start it
back up to get rid of all of the variable names we
have accumulated.
Let's make a little table to show state, identity and behavior of strings and lists.
String | ArrayList | |
---|---|---|
state | The string's character sequence | The objects in the list, their order, and the type of items in the list |
behavior | length() toUpperCase() toLowerCase() | size() get() set() |
The string methods you see here only depend on the state of the string object.
jshell> String s = "hello" s ==> "hello" jshell> s.toUpperCase() $2 ==> "HELLO" jshell> s.toLowerCase() $3 ==> "hello" jshell> s.length() $4 ==> 5
This method needs to be told the bounds of the substring you want. If you only specify one, it goes to the end of the string.
jshell> s.substring(1) $5 ==> "ello" jshell> s.substring(1,3) $6 ==> "el" jshell> s.substring(1,5) $7 ==> "ello"