background preloader

Java

Facebook Twitter

Stack & Heap. Exceptions in java. Overview Java has a powerful concept for exception and error handling.

Exceptions in java

An exception is an error that occurs at runtime. It is either generated by the Java Virtual Machine (VM) in response to an unexpected condition or it is generated by your code as a result of executing a throw statement. Understanding Exceptions Exceptions generated from runtime are called unchecked exceptions, since it is not possible for the compiler to determine that your code will handle the exception. Thrown exceptions are referred to as checked exceptions. How HashMap works in Java. How HashMap works in Java How HashMap works in Java or sometime how get method work in HashMap is common questions on Java interviews now days.

How HashMap works in Java

Almost everybody who worked in Java knows about HashMap, where to use HashMap or difference between Hashtable and HashMap then why this interview question becomes so special? Because of the depth it offers. It has become very popular java interview question in almost any senior or mid-senior level Java interviews.

Investment banks mostly prefer to ask this question and some time even ask to implement your own HashMap based upon your coding aptitude. Questions start with simple statement "Have you used HashMap before" or "What is HashMap? Almost everybody answers this with yes and then interviewee keep talking about common facts about HashMap like HashMap accept null while Hashtable doesn't, HashMap is not synchronized, HashMap is fast and so on along with basics like its stores key and value pairs etc.

Java - Differences between HashMap and Hashtable. Java Hashtable. Hashtable is an implementation of a key-value pair data structure in java.

Java Hashtable

You can store and retrieve a ‘value’ using a ‘key’ and it is an identifier of the value stored. It is obvious that the ‘key’ should be unique. java.util.Hashtable extends Dictionary and implements Map. Objects with non-null value can be used as a key or value. Key of the Hashtable must implement hashcode() and equals() methods. Java volatile keyword explained by example. We Recommend These Resources Overview volatile is probably the less known/understood/documented keyword in Java.

Java volatile keyword explained by example

I have recently read an article on one of my favourite blog about the volatile keyword. The author shows a piece of code where the volatile keyword seems to have an influence. This example was not easy to understand and the role of the volatile keyword on the behaviour of the JVM was not really defined. Basic Example The following show a basic example where volatile is required 01.public class VolatileTest { 02. private static final Logger LOGGER = MyLoggerFactory.getSimplestLogger(); 04. private static volatile int MY_INT = 0; 06. public static void main(String[] args) { 07. new ChangeListener().start(); 08. new ChangeMaker().start();

Java Singleton Design Pattern. Java has several design patterns Singleton Pattern being the most commonly used.

Java Singleton Design Pattern

Belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM. The class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes). A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.

One such scenario where it might prove useful is when we develop the help Module in a project. Singletons can be used to create a Connection Pool . To implement this design pattern we need to consider the following 4 steps: public class SingletonObjectDemo { // Note that the constructor is private private SingletonObjectDemo() { // Optional Code } } </b>*} public Object clone() throws CloneNotSupportedException { 10 Singleton Pattern Interview questions in Java - Answered. Singleton pattern in Java is one of the most common patterns available and it’s also used heavily in core Java libraries.

10 Singleton Pattern Interview questions in Java - Answered

Questions from Singleton pattern is very common in Java interviews and good knowledge of how to implement Singleton pattern certainly help.This is also one of my favorite design pattern interview question and has lots of interesting follow-up to dig into details , this not only check the knowledge of design pattern but also check coding, multithreading aspect which is very important while working for a real life application.

In this post have listed some of the most common question asked on Singleton pattern during a Java Interview. I have not provided the answers of these questions as they are easily available via google search but if you guys need I can try to modify this tutorial to include answers as well. As promised earlier and having received lot of request for providing answers of these question, I have decided to update this post along with answers. How Synchronization works in Java ? Example of synchronized block. Encapsulation. Encapsulation is one of the four fundamental OOP concepts.

Encapsulation

The other three are inheritance, polymorphism, and abstraction. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class.

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. Example: Let us look at an example that depicts encapsulation: The public methods are the access points to this class' fields from the outside java world. C++ Destructors Versus Java Finalization. How Garbage Collection works in Java.