Objects eligible for garbage collection

Let’s break this down line by line: CardBoard c1 = new CardBoard(); We now have two objects, the CardBoard c1 points at and the Short c1.story. Neither is available for GC as c1 points at the CardBoard and the story variable of the CardBoard points at the Short… CardBoard c2 = new CardBoard(); Similar to … Read more

Java – When is it a compiler error and when is it a runtime exception?

Compile time error – the java compiler can’t compile the code, often because of syntax errors. Typical candidates: missing brackets missing semicolons access to private fields in other classes missing classes on the classpath (at compile time) Runtime error – the code did compile, can be executed but crashes at some point, like you have … Read more

Priority queue ordering of elements

Actually internal data structure of PriorityQueue is not ordered, it is a heap. PriorityQueue doesn’t need to be ordered, instead, it focuses on head of data. Insertion is in O(log n) time. Sorting wastes time and useless for a queue. Moreover, either the element is-a Comparable, or a Comparator is provided. Unfortunately, non-comparable checking is … Read more

Java SneakyThrow of exceptions, type erasure

If you compile it with -Xlint you’ll get a warning: c:\Users\Jon\Test>javac -Xlint SneakyThrow.java SneakyThrow.java:9: warning: [unchecked] unchecked cast throw (T) ex; ^ required: T found: Throwable where T is a type-variable: T extends Throwable declared in method <T>sneakyThrowInner(Throwable) 1 warning That’s basically saying “This cast isn’t really checked at execution time” (due to type erasure) … Read more

Java unreachable catch block compiler error

A RuntimeException could be thrown by any code. In other words, the compiler can’t easily predict what kind of code can throw it. A RuntimeException can be caught by a catch(Exception e) block. IOException, however, is a checked exception – only method calls which are declared to throw it can do so. The compiler can … Read more

How many String objects will be created

“Fred” and “47” will come from the string literal pool. As such they won’t be created when the method is invoked. Instead they will be put there when the class is loaded (or earlier, if other classes use constants with the same value). “Fred47”, “ed4” and “ED4” are the 3 String objects that will be … Read more

What are “connecting characters” in Java identifiers?

Here is a list of connecting characters. These are characters used to connect words. http://www.fileformat.info/info/unicode/category/Pc/list.htm U+005F _ LOW LINE U+203F ‿ UNDERTIE U+2040 ⁀ CHARACTER TIE U+2054 ⁔ INVERTED UNDERTIE U+FE33 ︳ PRESENTATION FORM FOR VERTICAL LOW LINE U+FE34 ︴ PRESENTATION FORM FOR VERTICAL WAVY LOW LINE U+FE4D ﹍ DASHED LOW LINE U+FE4E ﹎ CENTRELINE … Read more