Skip to content

Commit 58bf766

Browse files
committed
Added content to Core Java
Updated myanswers,
1 parent 1fa379f commit 58bf766

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

topics/core/core-java.md

+65-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,82 @@
11
## Core Java
22

3+
This page focuses on oddities of Core Java from an interview perspective. For basic
4+
topics like inheritance, interfaces, etc. please read the book mentioned below.
5+
36
### Resources
47

5-
-
8+
- [OCA/OCP Java SE 7 Programmer I & II Study Guide](http://www.amazon.in/Programmer-Study-1Z0-803-1Z0-804-Certification/dp/0071772006)
9+
10+
### Default init values
11+
12+
- For fields (class level variables), values are auto assigned default values.
13+
- Method local variables should be manually assigned.
14+
- Default values (references = null, primitives = 0, boolean = false)
15+
- Arrays initialize its elements: int[] numbers = new int[10]; will assign all ints in the array to 0.
16+
17+
### String pool
18+
19+
- String constants are placed in a memory pool
20+
- When retrieved, returns reference to string in the pool.
21+
- Pool saves memory. New string constants with same value share same instance in the pool.
22+
- String is immutable thus these values are never changed. For any updates, new string constant is created.
23+
- String s = "abc" will place "abc" in pool and return its reference.
24+
- String s = new String("abc") will also place "abc" in pool, as well as allocate new memory
25+
- [implementation details](jvm-internals.md#string-interning)
26+
27+
### Wrapper class pool
628

7-
### Singleton
29+
- Boolean
30+
- Byte
31+
- Character from \u0000 to \u007f (7f is 127 in decimal)
32+
- Short and Integer from –128 to 127
833

9-
- static final variable (init guarantee)
10-
- Lazy loading (double checked)
11-
- Enums (by default lazy, and init guarantee)
34+
### Singleton options
35+
36+
- Using: static final variable (init guarantee)
37+
- Using: Lazy loading (double checked)
38+
- Using: Enums (by default lazy, and init guarantee)
1239

1340
### Override method rules
1441

1542
- Same method name and parameter types
1643
- Same or a subset of super methods' checked exceptions
1744
- Any number of runtime exceptions
18-
- Same or covariant return type
45+
- Same or covariant return type
46+
47+
### Covariant variables
1948

20-
### Varargs, boxing, upcast
49+
- Variable types which are compatible.
50+
- Eg: an int is covariant of long
51+
- Eg: an Lion class is covariant of Animal class (only if Lion extends Animal)
52+
- Can be used in parameters, return types or assignments
2153

22-
-
23-
-
24-
-
54+
### Varargs, boxing, widening
2555

56+
- Primitive Widening > Boxing > Varargs [example](http://stackoverflow.com/a/2128068/3494368)
57+
- Widening then Boxing not allowed.
58+
- Boxing then Widening allowed.
59+
- Widening between wrapper classes not allowed (eg: Long n = new Integer(10); not allowed)
60+
2661
### Inner classes
2762

28-
- Static inner class
29-
- Inner class
30-
- Method local inner class
31-
-
63+
Personally I find this part of Java to be super annoying, unnecessary and hardly ever used in real-life (especially after Java 8).
64+
Also, this topic does not come up a lot in interviews, so just skimp through.
65+
66+
- Inner class: Can access enclosing class's variables (even private ones)
67+
- Method local inner class: Same as above. Plus, it can access final variables in encapsulating method.
68+
- Anonymous inner class: Just no name, otherwise same as above.
69+
- Static inner class: No special relationship with outer class.
70+
71+
### Reference types
72+
73+
- **Weak reference** - Eligible for GC if object not referenced by any other variables. Good for caches. Are enqueued in ReferenceQueue just before GC (object can be resurrected in finalize). Returns null once object is eligible for GC, even if object is resurrected, the weak-reference still is dead, and will return null.
74+
- **Soft reference** - Same as above, but its GC’ed only when memory is low. Excellent for caches.
75+
- **Phantom reference** - Even after GC, it references the object, until the memory is reclaimed. Enqueued in ReferenceQueue after complete reclamation. Always returns null, so that you cannot resurrect it. Can be helpful to check when memory is reclaimed so that you can load next large object.
76+
- **WeakHashMap** - Weak keys. Removes entry once key is GC’ed.
77+
78+
### Cloning
79+
80+
- clone method (protected) of Object class returns shallow copy. Need to be explicitly cast back.
81+
- Requires class to implement Cloneable marker interface. Else returns CloneNotSupportedException
82+
- [More details](effective-java.md#clone)

topics/opinion/myanswers.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- In JIT - code is converted at runtime, just before
55
- Advantage: it can check availability of resources (RAM, CPU etc) and decide on corresponding optimizations
66
- Advantage: it can keep running for a while a find hot snippets of code, and optimize them even further. Eg: Android 7
7+
- Disadvantage: introduces latency during startup (for conversion)
8+
- Disadvantage: have to perform conversion every time during application start (cannot save and re-use the best optimization results)
79

810
## What do you like about Java
911

@@ -16,11 +18,11 @@
1618
## What do you not like about Java
1719

1820
- Verbosity - It is very difficult to write succinct code in Java. Too much boilerplate. Slightly subsidised by lambdas.
19-
- Threads could be less expensive akin to Go
21+
- Threads could be less expensive akin to Go channels or Kotlin co-routines.
2022
- Message passing and Immutability are not baked-in.
21-
- Thread communication by sharing memory is brittle (that is why so many locks are introduced), difficult to developers at all skill level to write correct code.
23+
- Thread communication by sharing memory is brittle (that is why so many locks are introduced), difficult to developers at all skill levels to write correct code.
2224
- I wish primitives could be directly added to collections and used as list
23-
- Null (?.) (Kotlin fix … i have spent so much time fixing NPE and wrote so much code to avoid it). Especially in finance world where POJOs have deeply nested structure. Doing a.getB().getC().getD() cannot be written. Every object needs to be null checked.
25+
- Null-checks (no ?. operator). Lot of boilerplate code and time wasted in checking for nulls. Especially in finance industry where POJOs have deeply nested structure. Doing a.getB().getC().getD() cannot be written. Every object needs to be null checked.
2426
- No tuples (especially helpful in multiple return types which currently have to be put in HashMap)
2527
- In fact, just check the kotlin language list
2628
- All pojos have to write getter, setter, equals, hash, clone/copy… kotlin fixes this

0 commit comments

Comments
 (0)