Block B

jshell

Python's interactive mode is called a REPL, which is short for read-evaluate-print loop.


>>> x =5 
>>> y = 3
>>> x*y
15

Java's REPL is called jshell. Here we invoke it in a command window.


MAC:Tue Mar 22:09:33:~> jshell
|  Welcome to JShell -- Version 17.0.1
|  For an introduction type: /help intro

jshell> int x = 4
x ==> 4

We created an integer variable and have it storing the value 4. Now let us evaluate an expression.


jshell> x*x
$2 ==> 16

What is this $2? It is called a scratch variable. If you don't store a result, jshell will create a scratch variable for you. You can then use these scratch variables.


jshell> $2 + x
$3 ==> 20

The special command /vars allows us to see our symbol table. Note the types of variables are recorded in the symbol table. You can see our scratch varialbes are also listed.


jshell> /vars
|    int x = 4
|    int $2 = 16
|    int $3 = 20

Here I create another scratch variable by failing to store my evaluation.


jshell> "amanaplanacanalpanama"
$4 ==> "amanaplanacanalpanama"

jshell> /vars
|    int x = 4
|    int $2 = 16
|    int $3 = 20
|    String $4 = "amanaplanacanalpanama"

Want to see your commands?


jshell> /list

   1 : int x = 4;
   2 : x*x
   3 : $2 + x
   4 : "amanaplanacanalpanama"

Now let's PUMP IT UP.


jshell> int p = 1
p ==> 1

jshell> p *= 1048576
$6 ==> 1048576

jshell> p
p ==> 1048576

jshell> p *= 1048576
$8 ==> 0

jshell> p *= 1048576
$9 ==> 0

jshell> p
p ==> 0

Uh oh. Is this really true? (1048576)3 = 0?? You are seeing the effects of type overflow. Yeah, watch the videos.

Here is how to see the biggest integer in Java.


jshell> int big = Integer.MAX_VALUE;
big ==> 2147483647

Now add 1.


jshell> big + 1
$12 ==> -2147483648

The type int is a 32 big two's complement integer. The largest int is 231 = 1. Add 1 and doughnut around to the lowest int which is -231.

Long for something bigger?



jshell> long y = 1;
y ==> 1

jshell> y
y ==> 1

jshell> y *= 1048576;
$15 ==> 1048576

jshell> y
y ==> 1048576

jshell> y *= 1048576;
$17 ==> 1099511627776

jshell> y *= 1048576;
$18 ==> 1152921504606846976

Oh. Bust.


jshell> y *= 1048576;
$19 ==> 0

Here is how to see the biggest long value


jshell> Long.MAX_VALUE
$20 ==> 9223372036854775807

That's 263 - 1.

Java has a byte type that is only one byte long. Doughnut city comes at 127.


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

jshell> b
b ==> -128

And here's the short of it.


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

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

jshell> s
s ==> -32768

A short is a 16-bit integer.

Here we can see the byte type's range


jshell> Byte.MAX_VALUE
$30 ==> 127

jshell> Byte.MIN_VALUE
$31 ==> -128

We have now seen the four integer types displayed in the table on the main page.

Warning, Will Robinson!


jshell> byte q = 1
q ==> 1

jshell> byte r = 1
r ==> 1

jshell> byte s = q + r
|  Error:
|  incompatible types: possible lossy conversion from int to byte
|  byte s = q + r;
|           ^---^

Arithmetic


jshell> 3 + 5
$34 ==> 8

jshell> 3*5
$35 ==> 15

jshell> 4  - 6
$36 ==> -2

jshell> 365/7
$37 ==> 52

jshell> 365 % 7
$38 ==> 1

Note that integer division is the default for integers.



jshell> 
jshell> 48*3 < 150
$39 ==> true
Java has a boolean type, boolean.


jshell> boolean q = 2 + 2  == 4;
q ==> true

jshell> q
q ==> true

Beware of floating-point hazards.


jshell> 2.0/8
$42 ==> 0.25

jshell> .1 + .1 +.1 == .3
$43 ==> false

jshell> .1 + .1 +.1
$44 ==> 0.30000000000000004

jshell> .3
$45 ==> 0.3

Here is how to cast an integer to a double.


jshell> (double) 5
$46 ==> 5.0

Amputations are performed here.


jshell> (int) 5.34
$47 ==> 5

jshell> (int) -5.34
$48 ==> -5

jshell> Math.floor(-5.34)
$49 ==> -6.0

Java is strict about type.



jshell> int bomb = 4.5;
|  Error:
|  incompatible types: possible lossy conversion from double to int
|  int bomb = 4.5;
|             ^-^
jshell> byte b = 675
|  Error:
|  incompatible types: possible lossy conversion from int to byte
|  byte b = 675;
|           ^-^

If you cast, YOU are responsible for the consequences.


jshell> byte b = (byte)675;
b ==> -93

jshell> byte b = (byte)676;
b ==> -92

This is downcasting: It can leave you downcast.

Java has two floating-point types, float and double. We will only use double in this class.



jshell> Float.MAX_VALUE
$52 ==> 3.4028235E38

jshell> Double.MAX_VALUE
$53 ==> 1.7976931348623157E308

Yes, there are strings. They are not primitive.


jshell> String s = "hello"
s ==> "hello"

jshell> s.charAt(0)
$55 ==> 'h'

jshell> String t = s.charAt(0);
|  Error:
|  incompatible types: char cannot be converted to java.lang.String
|  String t = s.charAt(0);
|             ^---------^

There is a separate charcter type.


jshell> char ch = 'a';
ch ==> 'a'

jshell> ch++
$57 ==> 'a'

jshell> ch
ch ==> 'b'

jshell> ch++
$59 ==> 'b'

YOu can cast betwenn char and int.


jshell> ch
ch ==> 'c'

jshell> (int) ch
$61 ==> 99

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

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

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

jshell> (char) 949
$65 ==> 'ε'

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

jshell> (char) 5545
$67 ==> 'ᖩ'

jshell> (char) 8223
$68 ==> '‟'

jshell> (char) 2211
$69 ==> 'ࢣ'

Primitives can't be dotted.


jshell> int quack = 6;
quack ==> 6

jshell> quack.foo()
|  Error:
|  int cannot be dereferenced
|  quack.foo()
|  ^-------^