Saturday, 31 March 2012

instanceof operator in Java

The instanceof operator compares an object to a specified type. You can use it to test at run-time if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The syntax of the instanceof operator is
Object_reference instanceof ClassName/InterfaceName

The instanceof operation returns boolean value ,either true or false.

The instanceof returns true, if the object referred to by the variable on the left side of the operator passes the IS-A test for the class or interface type on the right side.
Even if the object being tested is not an actual instantiation of the class type on
the right side of the operator, instanceof will still return true if the object being
compared is assignment compatible with the type on the right.


At compile time it checks whether the type of reference variable on the left and the class or interface on the left are in the same inheritance hierarchy.Else compile-time error occurs.For example

Thursday, 29 March 2012

ClassCastException in Java

public class ClassCastException
extends RuntimeException

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. i.e.Thrown when attempting to cast a reference variable to a type that fails the IS-A test.

Before discussing more about ClassCastException let us go through reference variable casting.

Reference Variable Casting

Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.The compatible data types means they should follow the following contracts.

1. Both the reference variables types should be in the same inheritance hierarchy.Else compile-time error occurs.
2. When attempting to cast a reference variable to a type that should pass the IS-A test.Else run-time error occurs ,i.e. throws ClassCastException.

Sunday, 25 March 2012

How and Why to override the toString() method in Java

Object class has following five non final methods.

1. clone()
2. equals(Object obj)
3. finalize()
4. hashCode()
5. toString()

Every Java class has Object as a super class so by default all above methods are provided in every java class. In this article we will discuss in detail the concepts of toString() method. We will discuss why it is necessary to override toString method and how we can override it.

The toString() method in the Object class is used to display some information regarding any object.The toString method is widely implemented. It provides a simple, convenient mechanism for debugging classes during development.When debugging, you want to be able to print a human readable representation of your object.It is widely used for logging.
The toString() method of an object gets invoked automatically, when an object reference is passed in the System.out.println() method.



Friday, 23 March 2012

How HashMap works in Java ?

In computer science, a hash table or hash map is a data structure that uses a hash function to map identifying values, known as keys (e.g., a person's name), to their associated values (e.g., their telephone number). Thus, a hash table implements an associative array. The hash function is used to transform the key into the index (the hash) of an array element (the slot or bucket) where the corresponding value is to be sought.

Ideally, the hash function should map each possible key to a unique slot index, but this ideal is rarely achievable in practice (unless the hash keys are fixed; i.e. new entries are never added to the table after it is created). Instead, most hash table designs assume that hash collisions—different keys that map to the same hash value—will occur and must be accommodated in some way.The Java uses

Separate chaining collision resolution strategy to handle such events.

Wednesday, 21 March 2012

Overriding hashCode() method in Java or Why always override hashcode() if overriding equals()?

Object class has following five non final methods.

1. clone()
2. equals(Object obj)
3. finalize()
4. hashCode()
5. toString()

Every Java class has Object as a superclass so by default all above methods are provided in every java class. In this article we will discuss in detail the concepts of hashCode() method. We will discuss why it is necessary to override hashCode method,Why always override hashCode() if overriding equals() and how we can override them.

Default implementation of hashCode() in Object class

The signature of hashCode is:

public int hashCode()