June 30 2015

Finally block in java

Finally block in java              

A try and catch block can have optional finally block which is always executed whether an exception is thrown or not. Finally block is generally used to release the memory, close the files and connections regardless of what happens in the try block.

If try blocks executes without any exception, finally block will be executed immediately after the try block. If there is an exception occurred in try block, finally block will be executed after matching catch block execution. Continue reading

June 26 2015

Interface example in java

Interface example in java

An interface is 100 % abstract class. It cannot be instantiated.  Interface is created when all the methods in the class have different behavior for subclasses and to enforce the subclasses to provide the implementation according to specific type.

An interface is a contract. The classes which are implementing the interface has to provide implementation for all the methods of interface. It can be used to achieve loose coupling.

To create an interface, use the interface keyword instead of the class keyword. To make a class that conforms to an interface, use the implements keyword instead of extends.

public interface EmployeeDao {

// declaration of methods

} Continue reading

June 24 2015

Java abstract class example

Java abstract class example

Abstract class

An abstract class cannot be instantiated. If a class contains one or more abstract methods, the class has to be declared as abstract. The first concrete (non-abstract) class which is extending the abstract class must implement all the abstract methods. An abstract class can have both abstract and non-abstract methods.

public abstract class Employee {  }

Abstract method

Abstract method is a method with only declaration and without implementation. Abstract method ends with a semi colon instead of curly braces. Abstract methods are created when there is different behavior for subclasses and to enforce the subclasses to provide the implementation according to specific type. Abstract methods cannot be private. Continue reading

May 28 2015

instanceof operator in java

instanceof operator in java

The instanceof operator compares an object to a specific type. It returns true if the object is an instance of the named class or class’s super classes otherwise it returns false. The instanceof operator can also be used for interfaces, it returns true if object implements interface otherwise false.

The left operand should be the object and right operand should be the class or interface.

“answersz.com” instanceof String

You cannot use the instanceof operator to test different class. It will give you compilation error.

Example Continue reading

May 27 2015

Singleton design pattern in java

Singleton design pattern in java

Singleton is a design pattern that restricts the instantiation of a class to one object and provides a global point of access to it.

The singleton pattern is implemented in java by creating a class with a method that returns reference to the object.

Singleton class can be implemented following

  • Declaring constructor as private to restrict creation of instance in other classes
  • Creating private static object reference of the class and this is the only instance of the class
  • Creating static method that returns the static object reference of the class. This method has to be called to get the instance of the class

Eager initialization

The single instance is created at the time of class loading Continue reading

May 25 2015

Java override toString

Java override toString

toString method is part of Object class. When you pass an object reference to the System.out.println() method, the object’s toString() method is called.

The toString method for Object class returns string with class name followed by @ symbol, followed by unsigned hexadecimal representation of the object’s hash code.

Class Name+ ‘@’ + hash Code

You have to override toString method if you want to see something meaningful about object of your class.

public String toString()

Example: Without toString method Continue reading

April 20 2015

Java increment and decrement operators

Java increment and decrement operators

Increment (++) and decrement (–) operators are used to increment and decrement a variable by one.

x=x+1 ->  x++;

x=x-1 ->  x–;

Increment and decrement operators can be placed before (prefix) and after (postfix) the variable. Operator placed before or after the operand in the expression can result in different outcome.

Prefix:  ++x,–x

Postfix: x++, x–

Prefix operator is applied before the expression is evaluated whereas postfix operator is applied after the expression is evaluated.

Example Continue reading

April 9 2015

Java varargs example

Java varargs example

Varargs allows us to create methods that can take variable number of arguments. The varargs allows the method to accept zero or multiple arguments. Varargs can be referred as variable arguments or var-args.The feature of variable argument has been added in Java 5.

Syntax

varargs parameter can be declared using ellipsis (…)

datatype … variable name

The below method accepts a String parameter and zero or multiple arguments of type int.

calculate(String name, int… marks) Continue reading

March 30 2015

Private Constructor In Java

Private Constructor In Java

Constructor is a special method in java which is used to initialize the instance variables of the class. A constructor has the same name of the class and does not have any return type (not even void).

Constructors can have any access specifiers (public, protected, default and private).If you declare a constructor as private, only code within the class itself can instantiate an object.

If you want to prevent the creation of instance in other class, private constructor can be used.The common cases of using the private constructors are

  •  Classes with only static methods
  •  Classes with only constants
  •  When implementing singleton pattern
  •  Enums

Example

Continue reading