Spaces:
Running
Running
Create 4. Match expressions and results
Browse files
Week 1: Types, condition clauses and loops/4. Match expressions and results
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Java's basic types
|
| 2 |
+
Type Saved information Number range
|
| 3 |
+
byte integer 1 byte
|
| 4 |
+
short integer 2 bytes
|
| 5 |
+
char character One Unicode-system character
|
| 6 |
+
int integer 4 bytes
|
| 7 |
+
long integer 8 bytes
|
| 8 |
+
float floating point number (i.e decimal number) 4 bytes
|
| 9 |
+
double floating point number 8 bytes
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
eg
|
| 13 |
+
public class Example {
|
| 14 |
+
public static void main(String[] args) {
|
| 15 |
+
String name = "Mike Madeup";
|
| 16 |
+
int age = 23;
|
| 17 |
+
double height = 171.25;
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
eg
|
| 24 |
+
// boolean (IN LOWERCASE) - true, false
|
| 25 |
+
public class Example {
|
| 26 |
+
public static void main(String[] args) {
|
| 27 |
+
boolean True = true;
|
| 28 |
+
boolean False = false;
|
| 29 |
+
boolean first_larger = 10 > 3;
|
| 30 |
+
|
| 31 |
+
System.out.println(first_larger);
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
Output:
|
| 35 |
+
true
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
eg
|
| 41 |
+
public class Example {
|
| 42 |
+
public static void main(String[] args) {
|
| 43 |
+
int first = 23;
|
| 44 |
+
int second = 10;
|
| 45 |
+
|
| 46 |
+
int multiply = first * second;
|
| 47 |
+
int sum = first + second;
|
| 48 |
+
|
| 49 |
+
int largeNum = (first + second) * 100;
|
| 50 |
+
|
| 51 |
+
System.out.println(multiply);
|
| 52 |
+
System.out.println(sum);
|
| 53 |
+
System.out.println(largeNum);
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
Output:
|
| 57 |
+
230
|
| 58 |
+
33
|
| 59 |
+
3300
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
eg
|
| 68 |
+
//If there are several different types involved in an expression, the type of the result is determined by the "largest" type, for example:
|
| 69 |
+
|
| 70 |
+
public class Example {
|
| 71 |
+
public static void main(String[] args) {
|
| 72 |
+
int integer = 10;
|
| 73 |
+
double float = 5.0;
|
| 74 |
+
|
| 75 |
+
System.out.println(integer * 2);
|
| 76 |
+
System.out.println(float * 2);
|
| 77 |
+
System.out.println(integer * 2.0);
|
| 78 |
+
System.out.println(integer + integer);
|
| 79 |
+
System.out.println(integer + float);
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
Output:
|
| 83 |
+
20
|
| 84 |
+
10.0
|
| 85 |
+
20.0 // int * float -> float
|
| 86 |
+
20
|
| 87 |
+
15.0 //int + float -> float
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
eg
|
| 95 |
+
//division
|
| 96 |
+
public class Example {
|
| 97 |
+
public static void main(String[] args) {
|
| 98 |
+
System.out.println(5 / 2);
|
| 99 |
+
System.out.println(5 / 2.0);
|
| 100 |
+
System.out.println(5.0 / 2);
|
| 101 |
+
System.out.println(5.0 / 2.0);
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
Output:
|
| 105 |
+
2 // int/int -> int
|
| 106 |
+
2.5 // int/float -> float
|
| 107 |
+
2.5 // float/int -> float
|
| 108 |
+
2.5 // float/float -> float
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
eg
|
| 116 |
+
public class Example {
|
| 117 |
+
public static void main(String[] args) {
|
| 118 |
+
int divided = 10;
|
| 119 |
+
int divisor = 4;
|
| 120 |
+
|
| 121 |
+
// This returns and integer...
|
| 122 |
+
System.out.println(divided / divisor);
|
| 123 |
+
|
| 124 |
+
// Result is wrong here too...
|
| 125 |
+
double divide = divided / divisor;
|
| 126 |
+
System.out.println(divide);
|
| 127 |
+
|
| 128 |
+
//...but this works:
|
| 129 |
+
System.out.println(divided * 1.0 / divisor);
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
Output:
|
| 133 |
+
2 // int/int -> int (rounded down)
|
| 134 |
+
2.0 // int/int -> int (rounded down) -> then convert to double
|
| 135 |
+
2.5 // 'new' double/int -> double
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
eg
|
| 142 |
+
2 * (3 - 1) 4
|
| 143 |
+
6/4 1
|
| 144 |
+
2 * 3.0 + 1 7.0
|
| 145 |
+
2.0 * (3.0 - 1.0) 4.0
|
| 146 |
+
2 * 3 + 1 7
|
| 147 |
+
2 * (3 + 1) 8
|
| 148 |
+
2 * (3.0 + 1) 8.0
|
| 149 |
+
6/(2*2.0) 1.5
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|