-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjava8notes.txt
More file actions
279 lines (188 loc) · 11.2 KB
/
java8notes.txt
File metadata and controls
279 lines (188 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
3 comments type :
- single-line //
- multi-line /* */
- Javadoc /** */
Maximum one public class per file : matches the filename
Glossary:
- accessor method: Method used to read an instance variable (= getter)
- mutator method: Method used to change the value of an instance variable (= setter)
- access modifier: Defines what other code can refer to the code. Choices are private, protected, public, and default (no modifier)
- member: a field or method in a class
- field: = instance (non-static) variable in a class
- local variable: A variable defined within a method, including method parameters. Not initialized by default.
- mutable: an object that can change state
- immutable: an object that cannot be changed
- label: A pointer to the head of a statement that allows the application flow to jump to it or break from it. A label is a single-word expression followed by a colon (:).
- litteral: A numeric, character, boolean or String value that is typed in the code
- negation operator: (-) reverses the sign of a numeric expression
- ternary operator = conditional operator
- equality operator ==: works with 2 primitive types, boolean values, or object references are the same
- logical complement operator: given a boolean expression, the operator ! returns the reverse of it.
- numeric promotion: automatic upcast the data type of a smaller data type to a larger data type, as well as update an integral value to a floating point value
- overflow: when a number calue is too large to fit in a data type and the value automatically warps around to the lowest possible value, and counts up from there
- parameter: the name of the variable in the method signature that is assigned the VALUE of the argument (pass-by-value)
- property: What a JavaBean calls an instance variable
- string pool: An area in the JVM where string litterals are stored. The JVM can optimize the use of string litterals by allowing only one instance of a string in a pool.
- for each loop: iterate over Java built-in arrays or objects that implements java.lang.Iterable
- underflow: When a number value is too small to fit in a data type and the value automatically wraps around to the highest possible value, and counts down from there.
- update statement: the section of a basic for loop that is executed at the end of the loop before the boolean expression is evaluated again.
- break != continue
break: A statement within a switch, while, do-while, or for statement that causes the control flow to exit the structure and returns control to the enclosing statement.
continue: A statement within a while, do-while, or for loop that causes the flow to stop the execution of the ccurrent iteration and go on to the next.
- deferred execution: A situation in which there is a block of code that will be run later (eg. lambda)
_______________________________________
General questions:
- Be careful with negation (are not legal, does not compile, etc)
- Inmutable types:
- String
- Dates date.plusDays(2)
Be careful if two answers are expected. For example does not compile, and if fixed the solution would be
Which types can be passed as a parameter: be careful not to instantiate an abstract class
When there are 2 classes, make sure there is or not inheritance between them, that only one is declared as public
__________________________
Compilation error
Be careful to:
- Check no public
- Inheritance:
- Be careful to position of this(), even if part of code that are not called
- Date/Datetime
__________________________
Runtime error:
Be careful to:
public static void main(String... args)
______________________
Be careful
- public void eat(int piecesOfCheese){
int bitesOfCheese = 1;
}
2 local variables are declared
- a variable is local to its scope
- Be careful with immutable types: String
- Arrays:
- {true, true} is not a valid array declaration without new boolean[2]
int[] array1 = {1, 2, 3, 4, 5, 6, ,7, 8}; - working
array1 = {1, 1, 1, 1, 2, 5, ,7, 8}; - NOT working
array = new int[] {1, 1, 2, 3, 5, 8};
byte<short<int<long
float<double
- Local variables require initializing them before referencing
- Instance variables have a default value
- Package: folders underneath the current path. A java file can be built from any folder (Compile even if the package is not correct). Be careful at runtime.
- An assignation returns the value on the right
- Before checking references with ==, make sure the two variables are the same type, otherwise does not compile
- StringBuilder.substring() returns a string, does not update the variable (same behaviour as substring on a string)
- ArrayList: null does not require autoboxing
Unboxing null generates a NullPointerException
int value = (Integer) null;
If r is a reference of type Integer, then unboxing conversion converts r into r.intValue()
_______________________________________________
Chapter 1
- Identifier first name must be _ $ or a letter (not a digit)
- Class Object: protected void finalize()
a top-level class cannot be declared as static, otherwise does not compile
Only inner class can be declared as static.
A field, a method and a construct can share the same identifier
_______________________________________________
Chapter 2: Operators and Statements
- switch statement: A complex decision-making structure in which a single value is evaluated and control jumps to a particular case statement or default block and continues until a break statement is reached or the switch statement finishes. Switch values may be bytes, shorts, chars, ints, enums, as well as classes that wrap primitive data types.
double is not a valid data type for a switch
1.09 is not a valid value for a case
_______________________________________________
Chapter 3
- int to string: compilation error if an int is assigned to a String (event with explicit cast)
- Possible to concatenate a string "" and a char '' without any cast
- A list instantiated using generics will only accept the specified type and null (+ types than can be autoboxed)
- binarySearch: if not found returns - index - 1 (otherwise conflict with index 0)
- null for a List<Integer> does not require autoboxing
- A string can not be assigned to a StringBuilder sb = "foo"
StringBuilder constructor can take only String, char or int (int for capacity!).
append() is overloaded to take any type in parameter, including primitives
- += equivalent to an explicit cast (String a; a+=2 is valid)
It is not possible to chain on a collection (returns boolean) .add()
binarySearch implemented for Arrays and Collections
Arrays.binarySearch()
Collection.binarySearch()
int Integer.parseInt(String s)
Integer Integer.valueOf(String s)
Integer Integer.valueOf(int s)
It is possible to save an Integer in a int variable (since java 1.5)
equals
- Arrays: does not check the content, only reference equality
- Collections: content equality
Varargs:
- A list can not be passed as a varargs parameter. Use toArray() instead
- varargs (String...) can be called without any parameter ()
_______________________________________________
Chapter 4:
- Labels are not allowed for methods (not a statement)
- JavaBeans naming conventions: for a boolean: isPropertyName(), not get!
- It is possible to call a static method on:
- className
- instance of the class
- reference of the class assigned to null
- Difference between initializer and static initializer: if the class is never instantiated, the instance initializer is never called (ie: class that contains main method)
- Final attributes must be declared exactly once, in declaration OR in static initializer. Otherwise compilation error, plus one for each other line it is declared.
compilation error if declared 0 or > 1 times
- import static compiles but not static import
- this() can only be called as a first line of a constructor (not from a method)
- this() is only callable if the constructor is declared (not valid for default constructor)
- An instance object is needed to call this and super non-static variable super cannot be referenced from a static context
- Question: Is it possible to call the default constructor using this? this can only be called from a constructor inside the same class. If there is a user-defined constructor, there is no default one
- Lambdas:
- return and ; required only if {return 1;}
If type is specified, the parenthesis are required (String s)
- Be careful with Lambda parameters type: Check matching interface if needed
- Autoboxing does not work for predicates
- A void method can return nothing but not null or anything else
_______________________________________________
Chapter 5:
- inheritance: The process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class (no private)
If A contains a private method()
If B inherits A
a method in B cannot see method()
- A variable (even public) can be hidden but not overriden
- Abstract modifier is not implicitly applied to all methods since some can be declared as default
- A method with a body in an interface must be declared static or default
- An interface does not inherit fron Object
- Private methods can be hidden but not overriden: a reference of superclass type will always calll superclass method
Difference between:
- overload
- override
- hide
Be careful
A variable declared as Object can refer any variable whose class inherits Object
Any Object can not be passed as a parameter to a function taking an Object subclass parameter
final keyword in a method declaration to indicate that the method cannot be overridden by subclasses
Instance methods are preferred over interface default methods (case where extends one class implements another)
If a method is declared as abstract, it can not have a body declared. Pay attention to empty body {}
An interface may inherit two abstract methods with the same signature
All methods within abstract classes and interfaces are assumed to be abstract: wrong, an abstract class may contain concrete methods.
A class that implements an interface do not need to implement inherited methods if the class is abstract.
_______________________________________________
Chap6: Exceptions
Be careful, new Exception() does not raise the exception! The keyword throw is mandatory
- A method that declares an exception is not required to throw one
- Runtime exceptions/Errors can be thrown in any method
General exam questions:
Be careful with allowed/required:
Checked exceptions are allowed to be declared or thrown
Checked exceptions are required to be declared or thrown
Both seem correct according to correction
A method that declares an exception is not required to throw one
/!\ In a catch, an exception catched must be declared by one of the methods in the try, otherwise unreachable code
If two exceptions are thrown (the second in finally) only the second is displayed
Be careful: runtime Exception are not required to be declared: questions: which lines can be inserted so that the code compiles
Finally:
- No code after finally, otherwise compilation error unreachable code
- No finally before catch, otherwise compilation error catch without try
_______________________
To be sorted:
Autoboxing work for collections not inferring predicates???
_______________________
Questions:
import java.util.*;
import java.sql.*;
Does it compile if there is no call to Date?
Hiding/Overriding:
- q8
- q11 code to undestand