ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException and NoClassDefFoundError occur when a required class is not found in the classpath at runtime. In this tutorial, we resolve the conflict between these 2 exceptions and explain the different scenarios of reproducing each one of them. 1- ClassNotFoundException As per java doc, this exception is thrown when trying to instantiate a class using one of these methods: forName(), findSystemClass(),...

How to iterate a Map in Java

This tutorial shows several ways for iterating a Map in Java. 1- Entry Set The common way for iterating a Map in Java is through entrySet() method as the following:

2- Iterator You can also use entrySet() along with iterator() methods to iterate a Map through an Iterator:

3- keySet() Using keySet() method, you can retrieve all the...

How to iterate a List in Java

This tutorial shows several ways for iterating a List in Java. 1- Typical For loop Using a typical For loop, you can iterate a List as the following:

2- Enhanced For loop Since JDK 5.0, you can iterate a List using an enhanced For loop which works as the following:

3- Typical While loop Using a typical while...

ArrayList vs Vector

ArrayList and Vector collections are used interchangeably for storing several items with the same type inside the memory. In this tutorial, we list the major similarities and differences between these 2 collections. 1- Similarities Both collections implement List interface, hence they expose similar operations for add, edit and delete operations. Both store their items in a dynamic Array inside the memory. Both...

Hashtable VS HashMap

Hashing objects like HashMap and Hashtable are mostly used for storing and caching data as a key/value pairs inside the memory. In this tutorial, we list the major similarities and differences between these 2 data types. 1- Similarities Both expose the same interface for adding, removing and accessing elements from the memory. get(): retrieve an item by a key. remove():...

Java – Convert File to byte[]

This tutorial shows several ways to convert a File object to a byte[] array in Java. 1- Traditional way The traditional conversion way is through using read() method of InputStream as the following:

2- Java NIO With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO Besides JDK, you can...

Java – Convert byte[] to File

This tutorial shows several ways to convert a byte[] array to File in Java. 1- Traditional way The traditional way of doing the conversion is through using FileOutputStream as the following:

2- Java NIO With Java 7, you can do the conversion using Files utility class of nio package:

3- Apache Commons IO Besides JDK, you can do...

Top 15 Eclipse Keyboard Shortcuts for Java Developers

This tutorial shows the most commonly used keyboard shortcuts while developing a Java application using Eclipse IDE. Top 15 Eclipse shortcuts 1- CTRL-SHIFT-R: used to look for a specific resource in the work space, the resource could be (java class, XML, HTML, JS, .properties …etc). It’s allowed to use * in the search box if you’re not sure about the...

Download file from a Web Application using Servlet

This tutorial shows how to download a file from a web application using Servlet. A typical task in most web applications is to download files stored at the server side to the client’s machine, here below we provide the common way of doing this with Servlet. 1- Download File Servlet Below is an example of a typical Servlet which downloads files...

Convert JSON to Java Object

This tutorials shows 2 ways for converting a JSON string to a Java object. 1- Gson Gson is the most popular library for converting JSON string to Java objects. With Gson, you can do the conversion in one line as the following:

Some points to be considered when using Gson: If the JSON string holds an invalid object attribute, then Gson implicitly...