Posts

Java 5: Covariant Return Type

Before Java 1.5, when we override a Parent class method then we need to have same return type of method in child class.  From Java 5 onwards, we can override a method by changing its return type, but overriding method's return type should be a sub-type of overridden method return type. For example, class Parent {    public Parent getObject() {      return this;    } } class Child extends Parent {    public Child getObject() {      return this;   }    public void print() {      System.out.println("Child class method..!");   }   public static void main(String[] args){     new Child().getObject().print();   } }

Java 5: Varargs

Suppose you are creating a method, but you aren't sure how many arguments your method is going to accept. So, to address this problem var-args were introduced with Java 1.5 release.  Prior to Java 1.5, we need to create overloaded method to handle variable number of parameters.  Varargs is short name of variable length argument.  Syntax:  A variable-length argument is specified by three periods(…).  For Example, public static int sum(int ...i) {      // method body } Above syntax tells the compiler that the sum() method can take zero or more arguments of type int. class Test {  public static int sum(int ...i) {      int sum = 0;      for (int a: i)          sum+=a;      return sum;  }  public static void main(String []args){     System.out.println(sum(1,2,3));     System.out.println(sum());     System.out.println(sum(1,2));  } } Output: 6 0 3

Java 5: Static import

Static import is the feature introduced in java 1.5 release. By using static import, we can directly access static members and methods of another class without class name or object name. Let us understand this with an example, package java5.staticimport; import static java.lang.System.*; public class StaticImportDemo {     public static void main(String[] args) {         out.println("Hello..Welcome to Java 5");     } } Output: Hello..Welcome to Java 5

Java 5 (Tiger) Features

Following are some of the Java 1.5 release: Static import @override annotatio n Covariant return types For each loop Auto Boxing/UnBoxing Varargs Scanner class Generics Type safe Enums java.lang.concurrent package Annotations (Metadata)

Difference between "== operator" and "equals() method"

Image
== Operator: The "== operator" is used to compare primitives types and objects. While comparing primitives types like int, float, boolean "==" operator works fine. The "== operator" compare two objects based on memory reference . So "==" operator will return true, only if two references which it is comparing is pointing to the exactly same object otherwise "==" will return false. equals() Method: The equals() method has been defined in java.lang.Object class and used for comparing two objects on the basis of their contents. In java, if two objects are equal they should have equal hashcode as well. The equals() method has contract with hashcode() method in java that whenever you override equals() method you should also override the hashcode() method.  Default implementation of equals() method is similar to the == operator which returns true if you are comparing two references of same object. D

Method Overloading Vs Method Overriding

Method Overloading: A feature that allows a class to have two or more methods having same name but different argument is said to be Method Overloading . Two methods are said to be overloaded if and only if both methods having the same name but different argument types. Also known as Static Binding , Compile-Time Polymorphism and Early Binding. class Calculate { public void area(int a) {//circle System.out.println("Area of Circle = " + (3.14 * a * a)); } public void area(int l, int b) { //rectangle System.out.println("Area of Rectangle= " + (l * b)); } } In above example, we have created two overloaded methods, first area() method prints area of circle and second area() method prints area of rectangle. Method Overriding: The methods declared in parent class are by default available in the child class through the inheritance . But sometimes child may not be fully satisfied with the parent class method implementation, then the child is allow