Tuesday 30 April 2013

Access Specifiers and Visibility of data member

VisibilityPublicProtectedDefaultPrivate
Within the Same ClassYesYesYesYes
From a class within the same packageYesYesYesNo
From a subclass within the same packageYesYesYesNo
From a subclass outside the packageYesYes, through InheritanceNoNo
From a non-subclass outside the packageYesNoNoNo

Sunday 28 April 2013

Difference between equals() and ==

"equals()" and "==" are both used for checking the equality of two variables. 
A small difference is there though. 

  1. equals() checks for "value based equality" while "==" checks for reference equality.
  2. equals() is a method and can only be used in conjunction with an object while "==" is an operator and can be used for primitives and objects both
  3. equals() is a part of the Object class
  4. equals() can be overloaded and you can give your own meaning to it which is not possible with the "==" operator
  5. "==" when used with a primitive checks for the value help by the variables. On the other side when "==" is used with a reference variable it checks if the two reference variables point to the same object.

Saturday 27 April 2013

How Pass by Value works in Java ???

In Java Pass by Value is everywhere ... whenever you pass an argument to a method in Java it is passed as a value. 

In Java, the primitives datatypes as well as the Class objects are passed by value only. 
By value i mean a copy of the original.
Let's take up a small example ...

public int doSomething(int val1, int val2) {
      val1 += 5;
      val2 += 10;
      return val1 + val2;
}

public static void main(String... args) {
     int val1 = 5;
     int val2 = 10;
     System.out.println("val1 = " + val1 + " , val2 = " + val2);
     int val3 = doSomething(val1,val2);

    
System.out.println("val1 = " + val1 + " , val2 = " + val2 + " , val3 = " + val3);
}

 As you can see in this example the two syso's print the same value of val1 and val2 before and after they are passed to the doSomething()

Now, if we talk about Class Objects, a copy of the object is passed as an argument to the method and the original object remains unchanged.

Wednesday 24 April 2013

Difference between 'pass by value' and 'pass by reference'

Pass by Value - This means passing the actual value of the variable as an argument to some method.

Pass by Reference - This means passing the reference to the variable as an argument to some method.

Now the question ??? What does Java Use from among the two ??? 

Java is actually pass by value for all the variables within a single VM.

Tuesday 23 April 2013

How JavaBeans differ from POJO's ???

JavaBeans are governed by certain Java Specifications which a POJO is free from.

The JavaBean specification requires a java class to ...
  1. be Serializable
  2. have getters and setters
  3. have a no argument
JavaBean Property Naming Rules ...
  1. If the property is not a boolean, the getter method's prefix must be get e.g. getSize()
  2. If the property is a boolean, the getter method's prefix should be either get or is e.g. getEmpty(), isEmpty()
  3. The setter method's prefix must be set e.g. setSize()
  4. Name of the getter or setter method is completed by changing the case of the first letter of the property name to UPPERCASE e.g. if property is size then methods are getSize() and getSize()
  5. setter methods should be marked public with void return type with an argument that represents the property type
  6. getter methods should be marked public, take no arguments, and have a return type that matches the argument type of the setter method

Sunday 21 April 2013

What is a Plain Old Java Object or POJO ???

A "POJO" is an ordinary Java Object which does not follow any of the major Java object models, conventions, or frameworks.  

A "POJO" however is bound by the restrictions forced by the Java Language Specification.

Some of these restrictions are ... 

  1. A POJO cannot extend any Class
  2. A POJO cannot implement any Interface
  3. A POJO cannot contain any prespecified annotation 
An example of a simple POJO may be ...

public class POJO {
 
    private String someProperty;
 
    public String getSomeProperty() {
         return someProperty;
    }
 
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
}