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.
Here we create in integer variable that stores the value 4.
jshell> int x = 4;
x ==> 4
Now watch this.
jshell> int p = 1;
p ==> 1
jshell> p *= 1048576;
$3 ==> 1048576
jshell> p
p ==> 1048576
jshell> p *= 1048576;
$5 ==> 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.
Java's int
type is a bit signed integer in two's complement notation.
Let's go out for a byte
jshell> byte b = 0;
b ==> 0
jshell> while(b >= 0){ b++;}
jshell> b
b ==> -128
The byte
type is is an 8-bit integer.
jshell> short s = 0;
s ==> 0
jshell> while(s >= 0){s++;}
jshell> s
s ==> -32768
A short
is a two-byte or 16-bit integer.
jshell> Short.MAX_VALUE
$18 ==> 32767
jshell> Short.MIN_VALUE
$19 ==> -32768
Where there's a short
there's a long
.
jshell> Long.MAX_VALUE
$20 ==> 9223372036854775807
jshell> Long.MIN_VALUE
$21 ==> -9223372036854775808
Look at the creative use of scrach variables to visit the scene of the doughnut.
jshell> $21
$21 ==> -9223372036854775808
jshell> $21 + 1
$23 ==> -9223372036854775807
jshell> $21 - 1
$24 ==> 9223372036854775807
Are thre strings? Yes.
jshell> String s = "hello";
s ==> "hello"
We will talk about them in more detail in lab class. There is a
separate character type in Java, char
jshell> char bugs = 'z';
bugs ==> 'z'
jshell> bugs--;
$27 ==> 'z'
jshell> bugs
bugs ==> 'y'
There is fluidity between char
and
int
.
jshell> bugs -= 32;
$29 ==> 'Y'
jshell> bugs
bugs ==> 'Y'
jshell> (int) bugs
$31 ==> 89
jshell> (char) 97
$32 ==> 'a'
jshell> (char) 945
$33 ==> 'α'
jshell> (char) 946
$34 ==> 'β'
jshell> (char) 947
$35 ==> 'γ'
jshell> (char) 948
$36 ==> 'δ'
jshell> (char) 10000
$37 ==> '✐'
jshell> (char) 5688
$38 ==> 'ᘸ'
jshell> (char) 3345
$39 ==> ''
jshell> (char) 25
$40 ==> '\031'
jshell> (char) 122
$41 ==> 'z'
Boola! Boola!
jshell> boolean foo = 4*4 == 16
foo ==> true
jshell> foo
foo ==> true
Uh oh.
jshell> not true
| Error:
| ';' expected
| not true
| ^
| Error:
| cannot find symbol
| symbol: variable not
| not true
| ^-^
Bang a boolean to negate it.
jshell> !foo
$44 ==> false
jshell> !true
$45 ==> false
jshell> !false
$46 ==> true
And??
jshell> true and true
| Error:
| ';' expected
| true and true
| ^
| Error:
| not a statement
| true and true
| ^-^
| Error:
| ';' expected
| true and true
| ^
Do this.
jshell> true && true
$47 ==> true
jshell> true && false
$48 ==> false
jshell> false && true
$49 ==> false
jshell> false && false
$50 ==> false
Prospectors, we've struck or
!
jshell> true || true
$51 ==> true
jshell> true || false
$52 ==> true
jshell> false || true
$53 ==> true
jshell> false || false
$54 ==> false
Here is exclusive or.
jshell> true ^ true
$55 ==> false
jshell> true ^ false
$56 ==> true
jshell> false ^ true
$57 ==> true
jshell> false ^ false
$58 ==> false
Verboten, unlike Python.
jshell> true + true
| Error:
| bad operand types for binary operator '+'
| first type: boolean
| second type: boolean
| true + true
| ^---------^
Java's double
is just like Python's float
.
jshell> double z = 5;
z ==> 5.0
jshell> z
z ==> 5.0
The float
type is a 32 bit floating point number.
jshell> Float.MAX_VALUE
$61 ==> 3.4028235E38
jshell> Double.MAX_VALUE
$62 ==> 1.7976931348623157E308
Watch for this old chestnut.
jshell> .1 + .1 + .1 == .3
$63 ==> false
jshell> .1 + .1 + .1
$64 ==> 0.30000000000000004
Here is our session's symbol table.
jshell> /vars
| int x = 4
| int p = 0
| int $3 = 1048576
| int $5 = 0
| int biggie = -2147483648
| int $8 = -2147483648
| int $10 = -2147483648
| byte b = -128
| short $18 = 32767
| short $19 = -32768
| long $20 = 9223372036854775807
| long $21 = -9223372036854775808
| long $23 = -9223372036854775807
| long $24 = 9223372036854775807
| String s = "hello"
| char bugs = 'Y'
| char $27 = 'z'
| char $29 = 'Y'
| int $31 = 89
| char $32 = 'a'
| char $33 = 'α'
| char $34 = 'β'
| char $35 = 'γ'
| char $36 = 'δ'
| char $37 = '✐'
| char $38 = 'ᘸ'
| char $39 = ''
| char $40 = '\031'
| char $41 = 'z'
| boolean foo = true
| boolean $44 = false
| boolean $45 = false
| boolean $46 = true
| boolean $47 = true
| boolean $48 = false
| boolean $49 = false
| boolean $50 = false
| boolean $51 = true
| boolean $52 = true
| boolean $53 = true
| boolean $54 = false
| boolean $55 = false
| boolean $56 = true
| boolean $57 = true
| boolean $58 = false
| double z = 5.0
| float $61 = 3.4028235E38
| double $62 = 1.7976931348623157E308
| boolean $63 = false
| double $64 = 0.30000000000000004