Block F

Eight bit integer


   11111111   is -1
  111
   00100000                    32
   11111111
   --------
   00011111                    31 

Uh oh.


(base) MAC:Mon Apr 12:14:10:HP> jshell
|  Welcome to JShell -- Version 15.0.1
|  For an introduction type: /help intro

jshell> x = 5
|  Error:
|  cannot find symbol
|    symbol:   variable x
|  x = 5
|  ^

In Java, variables have type. Here we create an integer variable x with value 5.


jshell> int x = 5;
x ==> 5


The type of a variable canoot be changed during its lifetime. Variables can only bind to objects of their own types.


jshell> x = "cowpies"
|  Error:
|  incompatible types: java.lang.String cannot be converted to int
|  x = "cowpies"
|      ^-------^

Now watch this.


jshell> x = 1;
x ==> 1

Here we see all of the variables we have created.


jshell> /vars
|    int x = 1

This looks preposterous! It's an infinite loop!


jshell> while(x > 0){ x = x + 1;}

Beezlebub! It's a dark conspiracy.


jshell> x
x ==> -2147483648

No, it is actually just doughnuts. Think of the integers as being a circle instead of along a line. The type int is a 32 bit signed integer in Twos Complement Noatation.


jshell> x = x - 1
x ==> 2147483647

jshell> x
x ==> 2147483647


jshell> byte b = 1;
b ==> 1

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

jshell> b
b ==> -128

Bytes are between -128 and 127.


jshell> short s = 1
s ==> 1

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

jshell> s
s ==> -32768

A short is a 16 bit signed integer.


jshell> long ell = 1;
ell ==> 1

jshell> for(int k = 0; k < 30; k++){ell *= 2;}

jshell> ell
ell ==> 1073741824

jshell> for(int k = 0; k < 30; k++){ell *= 2;}

jshell> ell
ell ==> 1152921504606846976

jshell> ell *= 2
$19 ==> 2305843009213693952

jshell> ell *= 2
$20 ==> 4611686018427387904

jshell> ell *= 2
$21 ==> -9223372036854775808

That number at the bottom is 263 - 1. Longs are 64 bit signed integers.

There are four integer types byte(8), short(16), int(32), long(64). Here we are making a string.


jshell> String s = "fooment";
s ==> "fooment"

jshell> s
s ==> "fooment"

Boola! Boola!


jshell> 4 < 5
$24 ==> true

jshell> 4 > 5
$25 ==> false

The boolean constants: true, false.

And now for a broken cast.


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

Floating point time.



jshell> double z = 4.22422
z ==> 4.22422

jshell> //64 bit floating point number (IEEE 754 standard)

jshell> float boat = 5.2f;
boat ==> 5.2

jshell> //32 bit floating point number (IEEE 754 standard)

Arithmetic is no surprise.



jshell> 4 + 5
$28 ==> 9

jshell> 4 + .3
$29 ==> 4.3

There is a separate character type that has some fluidity with integers. Use single quotes for character literals.



jshell> char x = 'a';
x ==> 'a'

Characters are unicode.


jshell> (int) x
$31 ==> 97

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

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

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

jshell> (char)5000
$35 ==> 'ᎈ'

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

jshell> (char)2000
$37 ==> 'ߐ'

jshell> (char)2001
$38 ==> 'ߑ'

jshell> (char)2002
$39 ==> 'ߒ'

jshell> (char)2003
$40 ==> 'ߓ'

jshell> (char)2004
$41 ==> 'ߔ'

You have met the eight primitive types.

Here are all of the local variables we now have. Note that replies from jshell are also given variables.


jshell> /vars
|    byte b = -128
|    long ell = -9223372036854775808
|    long $19 = 2305843009213693952
|    long $20 = 4611686018427387904
|    long $21 = -9223372036854775808
|    String s = "fooment"
|    boolean $24 = true
|    boolean $25 = false
|    double z = 4.22422
|    float boat = 5.2
|    int $28 = 9
|    double $29 = 4.3
|    char x = 'a'
|    int $31 = 97
|    char $32 = 'α'
|    char $33 = 'β'
|    char $34 = 'γ'
|    char $35 = 'ᎈ'
|    char $36 = 'ᖩ'
|    char $37 = 'ߐ'
|    char $38 = 'ߑ'
|    char $39 = 'ߒ'
|    char $40 = 'ߓ'
|    char $41 = 'ߔ'

jshell> $35
$35 ==> 'ᎈ'

Behold this magic.


jshell> s.toUpperCase()
$43 ==> "FOOMENT"

Ooh, this is not like Python.


jshell> s[0]
|  Error:
|  array required, but java.lang.String found
|  s[0]
|  ^--^

Do this. Indexing works just as it does in Python.


jshell> s.charAt(0)
$44 ==> 'f'

jshell> s.charAt(1)
$45 ==> 'o'

jshell>