Manual calculation: Abacus
Mechanical calculation: Cash register
Electromechanical calculation: Zuse
      Z3
Vacuum Tube: Eniac
Transistor: Microprocessor ICs
Z80 8-bit data bus
Processor Year Address/ data bus Transistors Clock rate
Intel 4004 1971 12 / 4 2,300 740 kHz
Zilog Z80 1976 16 / 8 8,500 2.5 MHz
Motorola 68020 1984 32 / 32 190,000 12.5 MHz
Processor Year Address/ data bus Transistors Clock rate
Six-core Opteron 2009 64 / 64 904,000,000 1.8 GHz
Core i7 Broadwell 2016 64 / 64 3,200,000,000 3.6 GHz
Apple's ARM M1 Ultra 2022 64 / 64 114,000,000,000 3.2 GHz

There are only 10 types of people in the world:

Those who understand binary and those who don't.

Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
Within limits: o.K. Caution: Overflow!
   010       2
  +011      +3
  ----     ---
   101       5
                  100       4
                  101      +5
               ------     ---
discarded ━━━▶ (1)001       1
by 3 bit
representation
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
Image layer 6
Within limits: o.K. Caution: Overflow!
   101      -3
  +010      +2
  ----     ---
   111      -1
  100     -4
  101     -3
 ----    ---
 1001      1
Signed 8 bit integer binary representation
Hotel key cards
7-bit
7-bit  with even parity bit
Western European characters: ISO Latin
      1 encoding
 UTF-8
      samples
  • Language Fundamentals
    • ➟ Primitive types
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
Image layer 6
Image layer 7
Image layer 8
Image layer 9
Image layer 10
Image layer 11
Image layer 12
Image layer 13
Name Bytes Type Range Literal samples
byte 1 Signed integer [ - 2 7 , 2 7 - 1 ] -
short 2 Signed integer [ - 2 15 , 2 15 - 1 ] -
int 4 Signed integer [ - 2 31 , 2 31 - 1 ] 0, 1, +1, -42, 0b1101, 017, 0xC
long 8 Signed integer [ - 2 63 , 2 63 - 1 ] 0L, 1L, +1L, -42L, 0b1101L, 017L, 0xCL
Name Bytes Type Range Literal samples
char 2 Unsigned integer [ 0 , 2 16 - 1 ] 'A', '*', '-', 'Ç', '⇶' ...
float 4 Floating point ± 1.18 × 10 - 38 to ± 3.4 × 10 38 3.14f, 0.022f, -3.4E-24f
double 8 Floating point ± 2.23 × 10 - 308 to ± 1.8 × 10 308 3.14, 0.022, -3.4E-24, 3.14d,...
boolean ? Logical value not applicable true, false
  • Language Fundamentals
    • ➟ Variables
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
// Variable declaration:
//     Variable's type is double
//     Variable's name is «pi» (identifier)

double pi;
{type name} {variable name} ;
double pi;      // Variable declaration

pi = 3.1415926; // Assigning value to variable

// Print a circle's area of radius 2.0

System.out.println(pi * 2.0 * 2.0);
Separate declaration and initialization
double pi;      // Declaration of variable pi

pi = 3.1415926; // Value assignment
Combined declaration and initialization
double pi = 3.1415926;
int a;
int b = 22;
int c;

being equivalent to either of:

Compact Multiple lines
int a, b = 22, c;  
int a,
    b = 22,  
    c;
  • Variable names.

  • Class names

  • Method names, e.g.:

    public static void main(String [] args)

Rules Legal Illegal
  • Start with a letter, _ or $, but not just a single _

  • May be followed by letters, digits, _ or $

  • Must not match:

  • $test

  • _$

  • count

  • blue

  • 2sad

  • _

  • switch

  • true

abstract     continue      for          new          switch
assert       default       if           package      synchronized
boolean      do            goto         private      this
break        double        implements   protected    throw
byte         else          import       public       throws
case         enum          instanceof   return       transient
catch        extends       int          short        try
char         final         interface    static       void
class        finally       long         strictfp     volatile
const        float         native       super        while
  • Start with a small letter like africa rather than Africa.

  • Use camel case e.g. myFirstCode.

    Variable naming conventions
  • Do not start with _ or $.

Modifier final prohibits changes:

final double PI = 3.1415926;
...
PI = 1.0; // Compile time error: Constant cannot be modified

Note

final variables by convention are being capitalized

Variable names are case sensitive:

int count = 32;
int Count = 44;
System.out.println(count + ":" + Count);

Resulting output:

32:44
Legal variable names
Correct:
double f;
f = -4.55;
Wrong:
f = -4.55;
double f;
int i = 2;
int j = i;   // o.K.: Assigning int to int
long l = i;  // o.K.: Widening conversion

i = l;       // Wrong: Narrowing

boolean b = true;
i = b;       // Error: int and boolean are incompatible types
i = 4.3345   // Error: Cannot assign double to int
i = "Hello"; // Even worse: Assigning a String to an int
byte b127 = 127; // o.K., static check
byte b128 = 128; // Wrong: Exceeding 127

Performing static range check

int a = 120;

byte b120 = a; // Error Incompatible types
               // Required: byte
               // Found:int

No static check on int variable's value

Benefits of final
long l = 4345;

int i = (int) l; // Casting long to int
System.out.println("i carrying long: " + i);

double d = 44.2323;
i = (int) d; // Casting double to int
System.out.println("i carrying double: " + i);
i carrying long: 4345
i carrying double: 44
long l = 3000000000000000L;
int i = (int) l;
System.out.println("i carrying long:" + i);

double d = 44300000000000.0;
i = (int) d;
System.out.println("i carrying double:" + i);
i carrying long:-296517632
i carrying double:2147483647
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5

«C» programming language miracles:

#include <stdio.h>

void main(void) {
  double measure = 65234.5435;
  short velocity;
  velocity = measure;
  printf("Velocity=%d\n", velocity);
}

Uups:

Velocity=-302
... and watch the outcome
Development costs:

~$7 billion.

Rocket and cargo

~$500 million.

related video and explanations

The cause of the failure was a software error in the inertial reference system.

Specifically, a 64 bit floating point number relating to the horizontal velocity of the rocket with respect to the platform was converted to a 16 bit signed integer.

The number was larger than 32,767, the largest integer possible in a 16 bit signed integer, and thus the conversion failed.

Related video explanation

  1. «C» vs. Java.
  2. Assignment and type safety
  1. Inventing tinyint.
  2. An int's minimum and maximum value
$test = 44;  # Assigning an int
print $test, "\n";

$test = "Jim"; # Assigning a string
print $test, "\n";

$cmp = 43.55; # A float

if ($test == $cmp) { # comparing string against float
   print "Equal\n";
} else {
   print "Different\n";
}
44
Jim
Different
$a = 2; # An integer
$b = 3; # Another integer

print '$a + $b = ', $a + $b, "\n";

$jim = "Jim";         # A string
print '$jim + $a = ' , $jim + $a, "\n";
$a + $b = 5
$jim + $a = 2
//Bad!
double pi = 3.141592653589793;
...
pi = -4; // Woops, accidential and erroneous redefinition
//Good
final double PI = 3.141592653589793;
...
PI = -4; // Compile time error:
         // Cannot assign a value to final variable 'pi'
Primitive type
int a = -15;

Possible types: All eight primitive Java types.

Reference type (based on classes)
GpsPosition start = new GpsPosition(48.7758, 9.1829);

Possible types: Arbitrary built-in or user defined classes.

GpsPosition start = new GpsPosition(48.7758, 9.1829);
String name = "Simon";
LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);
  • Language Fundamentals
    • ➟ Literals
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Code Result
System.out.println("Decimal "+ 35);
System.out.println("Binary " + 0b10_0011);
System.out.println("Hex "    + 0x23);
System.out.println("Octal "  + 043);
Decimal 35
Binary  35
Hex 35
Octal 35
System.out.println("35 as Binary (Base 2):       " + Integer.toString(35, 2));
System.out.println("35 as Ternary (Base 3):      " + Integer.toString(35, 3));
System.out.println("35 as Octal (Base 8):        " + Integer.toString(35, 8));
System.out.println("35 as Hexadecimal (Base 16): " + Integer.toString(35, 16));

results in:

35 as Binary (Base 2):       100011
35 as Ternary (Base 3):      1022
35 as Octal (Base 8):        43
35 as Hexadecimal (Base 16): 23
  1. Pretty may not be pretty
  2. Strange output
System.out.println(1000000000);    // o.K.
System.out.println(2147483647);    // o.K.: Largest int value 2^31 - 1

System.out.println(10000000000L);  // o.K.: Using type long
System.out.println(10000000000 );  // Compile time error: Integer number
                                   // larger than 2147483647 or 
                                   // 2^31 - 1, Integer.MAX_VALUE)
System.out.println("Hello");     // A String literal

System.out.println(33452);       // An int literal

System.out.println(34.0223);     // A double (floating point) literal

System.out.println(2147483648L); // A long literal
System.out.println("Value 1: " + 29);
System.out.println("Value 2: " + 0b11101);
System.out.println("Value 3: " + 0x1D);
System.out.println("Value 4: " + 035);
Value 1: 29
Value 2: 29
Value 3: 29
Value 4: 29
Literal Discriminator Type Value
29

base 10

Decimal 2 × 10 1 + 9 × 10 0
0b11101 0b, base 2 Binary

1 × 2 4 + 1 × 2 3 + 1 × 2 2

+ 0 × 2 1 + 1 × 2 0

0x1D 0x, base 16 Hexadecimal 1 × 16 1 + 13 × 16 0
035 0, base 8 Octal 3 × 8 1 + 5 × 8 0
byte, short -
char 'A', '\u0041'
int 29, 0b1_1101, 0x1D, 035, -29,
long 35L, 0b10_0011L, 0x23L, 043L, -35L,...
float 55.43F, 1.7E-23F, -17.F, 100_342.334_113F
double 55.43, 1.7 E -23, -17.
boolean true, false
String "Hello", "Greek Δ"
"Greek \u0394"
Arbitrary classes null
  1. Poor mans ASCII table
  2. Integer value hexadecimal representation
  3. Binary literals
  4. Testing the limits (Difficult)
  5. Why using braces in System.out.println(...) ?
  6. Composing strings of literals and variables
  7. Escaping double quotes
  8. Supplementary string exercises
int year = MMXIV; // Roman numerals representation

System.out.println("Olympic winter games: " + year);

Could this happen?

Olympic winter games: 2014
  • Language Fundamentals
    • ➟ Arithmetic limitations
byte count = 91;   // o.K.

int i = 91;

byte count2 = i;   // Compile error: Incompatible types
                   // Required: byte Found: int

byte points = 130; // Compile error: Incompatible types
                   // Required: byte Found: int
final int i = 91;

byte count = i;   //  o.K.
int count  = 2147483647;
int points = 2147483647;

int sum = count + points; 
System.out.println("Sum = "   +  sum);

Result:

Sum = -2
  01111111_11111111_11111111_11111111
+ 01111111_11111111_11111111_11111111
_____________________________________
  11111111_11111111_11111111_11111110
float float2Power31 = Integer.MAX_VALUE + 1f; // 2^31

float floatDoubleMAX_VALUE = 2 * float2Power31 * float2Power31 - 1f; // 2^63 - 1

System.out.format( "   Float value: %f\n", floatDoubleMAX_VALUE);
System.out.println("Expected value: "   +  Long.MAX_VALUE);

Result:

   Float value: 9223372036854776000.000000
Expected value: 9223372036854775807
    Difference:                 193
//Print 15 floating point digits
DecimalFormat df = new DecimalFormat("#.###############");

System.out.println(df.format(Float.intBitsToFloat(0b0_10000000_00001100110011001100110  )));

System.out.println(df.format(Float.intBitsToFloat(0b0_10000000_00001100110011001100111  )));
2.099999904632568
2.100000143051147
FloatConverter
  • Language Fundamentals
    • ➟ Conversions
Image layer 1
Image layer 2
Image layer 3
Image layer 1
Image layer 2
Image layer 3
Image layer 4
byte   b = 42;  // Narrowing: constant int literal to byte
short  s = b;   // Widening
int    i = s;   // Widening
long   l = i;   // Widening
float  f = l;   // Widening
double d = f;   // Widening
double d = 14.23;
float  f  = (float) d;  // Narrowing
long   l  = (long)  f;  // Narrowing
int    i  = (int)   l;  // Narrowing
short  s  = (short) i;  // Narrowing
byte   b  = (byte)  s;  // Narrowing
  1. int and char
  2. float vs. double
  3. int to char narrowing problems
  4. Get a byte from 139
  5. Ariane, I miss you!
  6. Reducing long to int (difficult)
  • Language Fundamentals
    • ➟ Operators and expressions
      • ➟ Arithmetic and logical operators
The binary plus operator
Binary operator output type
  1. Calculating a circle's area
  2. Dividing values
  3. Strange things with operator ++
  4. Adding values
  5. Representational float and double miracles
try {
    int sum = Math.addExact(2147480000, 2147480000);
    System.out.println("sum = " + sum);
} catch (ArithmeticException ex) {
    System.err.println("Problem: " + ex.getMessage());
}
Problem: integer overflow
double f = 34.3 / 0;
System.out.println("Value: " + f);
Value: Infinity

Watch out: Silent error!

Expressions involving infinity
Generic binary operator

Get a division's remainder:

int nuggets = 11,
    diggers = 3;

System.out.println("Nuggets per digger:" + nuggets / diggers);
System.out.println("Remaining nuggets:" + nuggets % diggers);
Nuggets per digger:3
Remaining nuggets:2
Left Right Out Examples
boolean boolean boolean |, &, &&, ||, ^
int int int

+, -, *, /, % (read here!)

int long long
byte byte int
double float double
int float float
char byte int
byte a = 1, b = 2;

byte sum = a + b; // Error: Incompatible types. 
                  // Required: byte
                  // Found: int
  1. int to short assignment
  2. int to short assignment using final
  3. Calculating a circle's area avoiding accidental redefinition
  4. Turning weeks into seconds
  5. Turning seconds into weeks
  6. Using predefined Java standard library constants
  7. Converting temperature values
  8. Time unit conversion
  9. Interest calculation
  10. Summing short and char

Boolean and of two operands:

boolean examSuccess = true,
        registered = false;

boolean examPassed = examSuccess & registered;

System.out.println("Exam passed:" + examPassed);
Exam passed:false
  1. Operator & vs. &&
  2. Strange addition
  • Language Fundamentals
    • ➟ Operators and expressions
      • ➟ Assignment operators

Increment variable by right hand value:

int a = 4;

a = a + 2;

System.out.println("Value:" + a);
int a = 4;

a += 2;

System.out.println("Value:" + a);
Value:6

Strange addition

Q: Execute the following snippet and explain its result:
Q: Execute the following snippet and explain its result:

Q:

Execute the following snippet and explain its result:

byte b = Byte.MAX_VALUE;
System.out.println(b);
b += 1;
System.out.println(b);

A:

In binary representation starting from Byte.MAX_VALUE we'd see:

  01111111         127 
 +       1       +   1
  --------        ----
  10000000         128

But a byte variable is being represented as 1-byte 2 complement:

  01111111         127 
 +       1       +   1 Overflow!
  --------        ----
  10000000        -128

Note that assigning b = b + 1 yields a compile time error instead:

Required type: byte
Provided: int

Conclusion: the += operator leads to a cyclic behaviour and thus differs from the ordinary + operator.

Logical and operation:

boolean examSuccess = true,
        registered = false;

examSuccess = examSuccess & registered;

System.out.println(
  "Exam success:" + examSuccess);
boolean examSuccess = true,
        registered = false;

examSuccess &= registered;

System.out.println(
  "Exam success:" + examSuccess);
Exam success:false
= Assign right to left operand
+= Assign sum of operands to left operand
-= Assign difference of operands to left operand
*= Assign product of operands to left operand
/= Assign quotient of operands to left operand
%= Assign remainder of operands to left operand
&= Assign logical and of operands to left operand
|= Assign logical or of operands to left operand
Understanding +=
  • Language Fundamentals
    • ➟ Operators and expressions
      • ➟ Unary operators
Increment variable by 1: Shorthand version:
int a = 4;

a = a + 1;

System.out.println("Value:" + a);
int a = 4;

a++;

System.out.println("Value:" + a);
Value:5
Increment variable by 1: Shorthand version:
byte value = 127; // Max possible value

value = value + 1; // Error: Required type: byte
                   // Provided:int

System.out.println(value);
byte value = 127; // Max possible value

value++; // o.K., will cycle through range

System.out.println(value);
Does not compile
Value:-128
Increment variable by 1: Shorthand version:
byte value = 127; // Max possible value

value = (byte)(value + 1); // cast required,
                           // possible overflow

System.out.println(value);
byte value = 127; // Max possible value

value++; // cycle through range,
         // no cast required. 

System.out.println(value);
Value:-128
pre-increment post-increment
int a = 4;
int b = ++a;
System.out.println(b);
int a = 4;
int b = a++;
System.out.println(b);
Output:
5
Output:
4
Three ways expressing the same
Shorthand Operation Explicit
//Integer operations
i--;    
i += k;
i %= 3;

// boolean
  - nothing appropriate -
    Decrement by one
    Raise by k's value
   assign modulus of 3



Switching true <--> false             
//Integer operations
i = i - 1;
i = i + k;
i = i % 3;

// boolean
b = !b;
  1. Guessing results
  2. Cleaning up the mess
  • Language Fundamentals
    • ➟ Comments
Multi line comment:
int a;
/*  We define a variable. Then
    subsequently a value is being assigned */
a = 33;
End of line comment:
int a;  // We define a variable.
a = 33; // Then subsequently a value is being assigned
int strength = a /* fixed value */ + b /* age */ + c /* courage */;

being run-time equivalent to:

int strength = a + b + c;
/**
 * Describing rectangles. 
 */
public class Rectangle {

  /**
   *
   * @param width Setting the rectangle's new width. 
   */         
              ┗━━━━━━━━━━━━━━━━━┓
                                
  public void setWidth(double width) {
      // Implementation yet missing
  }
  ...
}