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.

Within limits: o.K. Caution: Overflow!
```   010       2
+011      +3
----     ---
101       5```
```                  100       4
101      +5
------     ---
by 3 bit
representation```
Within limits: o.K. Caution: Overflow!
```   101      -3
+010      +2
----     ---
111      -1```
```  100     -4
101     -3
----    ---
1001      1```
• Language Fundamentals
• ➟ Primitive types
Name Bytes Type Range Literal samples
`byte` 1 Signed integer $math formula$ -
`short` 2 Signed integer $math formula$ -
`int` 4 Signed integer $math formula$ 0, 1, +1, -42, 0b1101, 017, 0xC
`long` 8 Signed integer $math formula$ 0L, 1L, +1L, -42L, 0b1101L, 017L, 0xCL
Name Bytes Type Range Literal samples
`char` 2 Unsigned integer $math formula$ 'A', '*', '-', 'Ç', '⇶' ...
`float` 4 Floating point $math formula$ to $math formula$ 3.14f, 0.022f, -3.4E-24f
`double` 8 Floating point $math formula$ to $math formula$ 3.14, 0.022, -3.4E-24, 3.14d,...
`boolean` ? Logical value not applicable `true`, `false`
• Language Fundamentals
• ➟ Variables
 ``````// 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:

• `boolean` or `null` literal

• `\$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`.

• 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`
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
 ```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```

«C» programming language miracles:

 ``````#include void main(void) { double measure = 65234.5435; short velocity; velocity = measure; printf("Velocity=%d\n", velocity); }`````` Uups: `Velocity=-302`
 Development costs: ~\$7 billion. Rocket and cargo ~\$500 million.

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.

 ``````\$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
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```
```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 $math formula$
0b11101 0b, base 2 Binary

$math formula$

$math formula$

0x1D 0x, base 16 Hexadecimal $math formula$
035 0, base 8 Octal $math formula$
 `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`
```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```
 ```DecimalFormat df = new DecimalFormat("#.###############"); //Print 15 floating point digits System.out.println(df.format(Float.intBitsToFloat(0b0_10000000_00001100110011001100110 ))); System.out.println(df.format(Float.intBitsToFloat(0b0_10000000_00001100110011001100111 )));``` ```2.099999904632568 2.100000143051147```
• Language Fundamentals
• ➟ Conversions
``````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
```try {
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!

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```

Boolean and of two operands:

 ```boolean examSuccess = true, registered = false; boolean examPassed = examSuccess & registered; System.out.println("Exam passed:" + examPassed);``` `Exam passed:false`
• 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`

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
• 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`
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;```
• Language Fundamentals
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
}
...
}```