Lecture notes |
Pdf slides |
(#1 of 5) |
Lecture notes |
Pdf slides |
(#2 of 5) |
Lecture notes |
Pdf slides |
(#3 of 5) |
Lecture notes |
Pdf slides |
(#4 of 5) |
Lecture notes |
Pdf slides |
(#5 of 5) |
Lecture notes |
Pdf slides |
(#1 of 6) |
Lecture notes |
Pdf slides |
(#2 of 6) |
Lecture notes |
Pdf slides |
(#3 of 6) |
Lecture notes |
Pdf slides |
(#4 of 6) |
Lecture notes |
Pdf slides |
(#5 of 6) |
Lecture notes |
Pdf slides |
(#6 of 6) |
Lecture notes |
Pdf slides |
|
(#1 of 13) |
Lecture notes |
Pdf slides |
|
(#2 of 13) |
Lecture notes |
Pdf slides |
|
(#3 of 13) |
Lecture notes |
Pdf slides |
|
(#4 of 13) |
Lecture notes |
Pdf slides |
|
(#5 of 13) |
Lecture notes |
Pdf slides |
|
(#6 of 13) |
Lecture notes |
Pdf slides |
|
(#7 of 13) |
Lecture notes |
Pdf slides |
|
(#8 of 13) |
Lecture notes |
Pdf slides |
|
(#9 of 13) |
Lecture notes |
Pdf slides |
|
(#10 of 13) |
Lecture notes |
Pdf slides |
|
(#11 of 13) |
Lecture notes |
Pdf slides |
|
(#12 of 13) |
Lecture notes |
Pdf slides |
|
(#13 of 13) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 5) |
Lecture notes |
Pdf slides |
|
(#2 of 5) |
Lecture notes |
Pdf slides |
|
(#3 of 5) |
Lecture notes |
Pdf slides |
|
(#4 of 5) |
Lecture notes |
Pdf slides |
|
(#5 of 5) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 5) |
Lecture notes |
Pdf slides |
|
(#2 of 5) |
Lecture notes |
Pdf slides |
|
(#3 of 5) |
Lecture notes |
Pdf slides |
|
(#4 of 5) |
Lecture notes |
Pdf slides |
|
(#5 of 5) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 4) |
Lecture notes |
Pdf slides |
|
(#2 of 4) |
Lecture notes |
Pdf slides |
|
(#3 of 4) |
Lecture notes |
Pdf slides |
|
(#4 of 4) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
int
literals
Lecture notes |
Pdf slides |
|
int
literals explained
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
float
to 2.1
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
(#1 of 3) |
Lecture notes |
Pdf slides |
|
(#2 of 3) |
Lecture notes |
Pdf slides |
|
(#3 of 3) |
Lecture notes |
Pdf slides |
|
(#1 of 4) |
Lecture notes |
Pdf slides |
|
(#2 of 4) |
Lecture notes |
Pdf slides |
|
(#3 of 4) |
Lecture notes |
Pdf slides |
|
(#4 of 4) |
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
%
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
byte
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
&
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
+=
operator
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
&=
operator
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
++
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
Lecture notes |
Pdf slides |
|
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 ------ --- discarded ━━━▶ (1)001 1 by 3 bit representation |
Within limits: o.K. | Caution: Overflow! | |
---|---|---|
101 -3 +010 +2 ---- --- 111 -1 |
100 -4
101 -3
---- ---
1001 1 |
Name | Bytes | Type | Range | Literal samples |
---|---|---|---|---|
byte |
1 | Signed integer | - | |
short |
2 | Signed integer | - | |
int |
4 | Signed integer | 0, 1, +1, -42, 0b1101, 017, 0xC | |
long |
8 | Signed integer | 0L, 1L, +1L, -42L, 0b1101L, 017L, 0xCL |
Name | Bytes | Type | Range | Literal samples |
---|---|---|---|---|
char |
2 | Unsigned integer | 'A', '*', '-', 'Ç', '⇶' ... | |
float |
4 | Floating point | to | 3.14f, 0.022f, -3.4E-24f |
double |
8 | Floating point | to | 3.14, 0.022, -3.4E-24, 3.14d,... |
boolean |
? | Logical value | not applicable | true ,
false |
|
{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);
double pi; // Declaration of variable pi
pi = 3.1415926; // Value assignment
double pi = 3.1415926;
int a;
int b = 22;
int c;
being equivalent to either of:
Compact | Multiple lines |
---|---|
|
|
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
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
double f;
f = -4.55;
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
|
Performing static range check |
|
No static check on |
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:
|
Uups: Velocity=-302 |
|
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
|
44 Jim Different |
|
$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'
GpsPosition start = new GpsPosition(48.7758, 9.1829);
String name = "Simon";
LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);
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 | |
0b11101 | 0b, base 2 | Binary |
|
0x1D | 0x, base 16 | Hexadecimal | |
035 | 0, base 8 | Octal |
int year = MMXIV; // Roman numerals representation System.out.println("Olympic winter games: " + year);
Could this happen?
Olympic winter games: 2014
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 |
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
try { int sum = Math.addExact(2147480000, 2147480000); System.out.println("sum = " + sum); } catch (ArithmeticException ex) { System.err.println("Problem: " + ex.getMessage()); }
Problem: integer overflow
|
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
int
to short
assignmentint
to short
assignment using final
Boolean “and” of two operands:
boolean examSuccess = true,
registered = false;
boolean examPassed = examSuccess & registered;
System.out.println("Exam passed:" + examPassed); |
Exam passed:false |
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 | Example using %= |
+= |
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 | Result=3 |
&= |
Assign logical “and” of operands to left operand |
|= |
Assign logical “or” of operands to left operand |
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; |
int a;
/* We define a variable. Then
subsequently a value is being assigned */
a = 33;
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 } ... }