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

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

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

Why does Double.NaN==Double.NaN return false?

NaN means “Not a Number”. Java Language Specification (JLS) Third Edition says: An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a … Read more

SCJP6 regex issue

\d* matches 0 or more digits. So, it will even match empty string before every character and after the last character. First before index 0, then before index 1, and so on. So, for string ab34ef, it matches following groups: Index Group 0 “” (Before a) 1 “” (Before b) 2 34 (Matches more than … Read more

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

From the JLS 5.2 Assignment Conversion In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int: – A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of … Read more