-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotes
More file actions
134 lines (110 loc) · 6.36 KB
/
Copy pathnotes
File metadata and controls
134 lines (110 loc) · 6.36 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
1. Java static key word:
Static keyword can be used with class, variable, method and block. Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without object.
2. Static Class
A class can be made static only if it is a nested class.
1) Nested static class doesn’t need reference of Outer class
2) A static class cannot access non-static members of the Outer class
3. @ means annotation in java
4. Serializable interface
5. Array vs ArrayList
Array: Simple fixed arrays that we create in Jave, like below
int arr[] new int[10]
ArrayList: Dynamic sized arrays in Java that implement List interface
ArrayList<Type> arrL = new ArrayList<Type>();
// A Java program to demonstrate differences between array
// and ArrayList
import java.util.ArrayList;
import java.util.Arrays;
class Test
{
public static void main(String args[])
{
/* ........... Normal Array............. */
int[] arr = new int[3];
arr[0] = 1;
arr[1] = 2;
System.out.println(arr[0]);
/*............ArrayList..............*/
// Create an arrayList with initial capacity 2
ArrayList<Integer> arrL = new ArrayList<Integer>(2);
// Add elements to ArrayList
arrL.add(1);
arrL.add(2);
// Access elements of ArrayList
System.out.println(arrL.get(0));
}
}
6. Throws vs Throw
1. throws clause is used to declare an exception and throw keyword is used to throw an exception explicitly.
2. If we see syntax wise then throw is followed by an instance variable and throws is followed by exception class names.
3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).
For example
throw
throw new Exception("You have some exception")
throw new IOException("Connection failed!!")
throws
public int myMethod() throws IOException, ArithmeticException, NullPointerException {}
4. You cannot declare multiple exceptions with throw. You can declare multiple exception e.g. public void method()throws IOException,SQLException.
5. checked exceptions can not be propagated with throw only because it is explicitly used to throw an particular exception. checked exception can be propagated with throws.
7. Use the string.equals(Object other) function to compare strings, not the == operator.
The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal.
8. UUID: Universally Unique Identifier, represents a 128-bit long value which guarantees uniquencess across time and space
9. ConcurrentHashMap
10. org.junit Annotation Type Before
When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class.
public class Example {
List empty;
@Before public void initialize() {
empty= new ArrayList();
}
@Test public void size() {
...
}
@Test public void remove() {
...
}
}
11. AtomicReference: An object that may be updated atomically
12. SerialVerisonUID
The serialization rutime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and the receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization
13. Type List vs type ArrayList in Java
Almost always the first one is preferred over the second one. The first has the advantage that the implementation of the List can change (to a LinkedList for example), without affecting the rest of the code. This will be a difficult task to do with an ArrayList, not only because you will need to change ArrayList to LinkedList everywhere, but also because you may have used ArrayList specific methods.
14. Differnce between C and Java returning an Object reference/pointer
The difference here isn't in the way the pointer is returned really. The difference is what you are actually returning. Or rather, what type of object you are trying to return. In the first example, you are returning a local variable. If you were actually allocating some memory dynamically, copying your characters into that block of memory, and then returning that - then you would be like what your Java example does; assuming the Java example is actually correct.
Code:
char *getName( void )
{
char buf[ BUFSIZ ] = { "some name" };
char *s = NULL;
s = malloc( strlen( buf ) + 1 );
strcpy( s, buf );
return s;
}
Something like that. Of course you need to free that when you are done.
15. How to return a value from try, catch and finally?
To return a value when using try/catch you can use a temporary variable, e.g.
double sum = 0.0;
try {
int length = values.length;
double arrayValues[] = new double[length];
for(int i = 0; i<length; i++) {
arrayValues[i] = Double.parseDouble(values[i]);
sum += arrayValues[i];
} catch(NumberFormatException e) {
e.printStackTrace();
} catch(RangeException e) {
throw e;
} finally {
System.out.println("Thank you for using the program!");
}
return sum;
}
Else you need to have a return in every execution path (try block or catch block) that has no throw.
16. scope
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
17. Java rounding up to an int using Math.ceil? int total = (int) Math.ceil(157/32); Why does it still return 4? 157/32 = 4.90625, I need to round up, I've looked around and this seems to be the right method.
You are doing 157/32 which is dividing two integers with each other, which always result in a rounded down integer. Therefore the (int) Math.ceil(...) isn't doing anything.