Java Exception Hierarchy

Java Exception Hierarchy As shown in the exception hierarchy, a Throwable class is a superclass of all errors and exceptions in Java. Objects those are instances of Throwable or one of its subclasses are thrown by JVM or by Java throw statement. Error: An Error is a subclass of Throwable that represents serious errors that can't be handled. A method is not required to declare throws clause for Error or any of its subclasses for

Read more 0

Type Erasure in Java

What is type Erasure? Generics in Java provide type checking at compile time and it has nothing to do at runtime. Java compiler uses Type Erasure feature to remove all generics type checking code (replace it with bound or Object if the type parameters are unbounded) in bytecode, insert type casting if necessary and generate bridge methods to preserve polymorphism in extended generic types. Here is an example of generic a class: class TypeErasureExample {

Read more 0

Wildcard - Java Generics

A wildcard can be used as a type of a parameter, field, local variable, and return type. Question mark (?) wildcard in generics and it represents the unknown type. Unbounded wildcard When we want our generic type to be working with all types, an unbounded wildcard can be used. For example: class UnboundWildcard { public static void UnboundWildcardPrint(List list) { list.forEach(listElement->{ System.out.println(listElement); }); } public static void main(String args) { List list = new ArrayList();

Read more 0

Java Generics

Generics are a feature of generic programming which was introduced in Java 5. This allows a type or method to work with various object types while providing compile-time type safety. For example, collection framework supports generics. We can use Hashset, ArrayList, HashMap etc. to store various type of objects, and while retrieving the data we don't need to type cast. In Java 5, java rewrote the collection framework to incorporate the generics. Here is an

Read more 0

Java Try With Resource

A resource is an object that must be closed once the program is finished or done using it, for example, file resource, JDBC database connection resource, socket connection resource etc. Prior to Java 7, there was no auto resource management, we open the resource in a try block and close the resource in finally block. For example: try{ //open resources }catch(IOException){ //Exception handling }finally{ //close resource } In the above approach there were few drawbacks:

Read more 0