Wednesday, July 13, 2011

Operator overloading in java ?

i  am going to explain operator overloading  on the basis of following context

1-language-defined  operator overloading
2-user-defined operator overloading

c++ support user defined operator overloading   only and java support language -defined  operator overloading

Java have language -defined  operator over loading properties only for + operator  .

+ operator is used to  add two primitive type variable and also used to concatenation string variable .in String concatenation it first check both operand ,if any of these String then it perform concatenation .

Example :
int i =10,j=10 k;
k=i+;j;
System.out.println(k);  //Out Put :20
String a="Hello";
String b="India";
String c=a+b;
System.out.println(c);  //Out Put:HelloIndia

Friday, July 8, 2011

What is difference between throw and throws key words in java ?

1-throws
i-throws is used by method to pass this exception to caller method and caller method should have to  handler this exception .it is used with method signature .
ii-the "throws" keyword is used in the method signature to declare that this method may throw an exception of the type specified or a sub-type of the one specified


Example :
public void passException()  throws Exception
{
     
}

2-throw
i-suppose you want to throw new type of your created Exception or already exists Exception then we used throw .it is used with statement .
ii-the "throw" keyword is used in the code of a method to perform the action of throwing an exception the value must be a proper sub-type of one of the exception types declared in the 'throws' clause of the method's signature


Example:
public void userException () throws MyException
{
    throw new MyException();
}

Tuesday, July 5, 2011

what is use of ordinal method of Enum class?

ProtoType of method is following .
public final int ordinal()


Method Name :ordinal
Return Type :int


It is final so no one override this method


Doc Specification :
/** * Returns the ordinal of this enumeration constant (its position * in its enum declaration, where the initial constant is assigned * an ordinal of zero). * * Most programmers will have no use for this method. It is * designed for use by sophisticated enum-based data structures, such * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. * * @return the ordinal of this enumeration constant */


Source Code :



package model;


public class Simple {
    public Simple() {
        super();
    }
enum Hello{     H1,H2;         }     public static void main(String[] args) {        Hello aa=Hello.H2;
        int i=aa.ordinal();
        System.out.println(i);
    }
}


the out put is :1
it just return the position of any enum variable .it starting point is zero same as Array .

Monday, July 4, 2011

private and final method overriding

I am going to explain the following case .

Case1:what happen when we override private method ?
Case2:what happen when we override final method ?
Case3:what happen when we override private with final method ?

Case1:what happen when we override private method ?
Explain :
Source code of Parent class
public class Parent {
public Parent() {
super();
}
private void hello(){
System.out.println("parent class");
}
}


Source code of Child class
public class Child extends Parent{
    public Child() {
        super();
    }
    private void hello(){
      System.out.println("child class method ");
    }}
Child class has same method but this is not called method overriding because the method is make private in Parent class .this is just a new method declaration .
SO do not confuse .compiler  does not complain you it run fine without any exception .

Case2:what happen when we override final method ?
Parent Class Code :
public class Parent {
    public Parent() {
        super();
    }
     public final void hello(){
      System.out.println("parent class");
    }
}
Child class Code :
  public class Child extends Parent{
    public Child() {
        super();
    }
    public  void hello(){
      System.out.println("child class method ");
    }
   }
If you going to run this it give you following compile time exception
Exception: hello() in model.Child cannot override hello() in model.Parent; overridden method is final
this time compiler know you are going to override final method which is not possible .so compiler  do not allow you  to redefine same method in child .
This is main difference between  private method and final method overriding.

Case3:what happen when we override private with final method ?
Parent Class :
public class Parent {
    public Parent() {
        super();
    }
     private final void hello(){
      System.out.println("Parent Class");
    }
}
Child Class :
public class Child extends Parent{
    public Child() {
        super();
    }
  private final void hello(){
    System.out.println("Child Class ");
  }
}
Here private is win over final .it means it's not going to give any compile  time exception .because we make Parent method as private which not inherit in Child method .if you define the method this way it meas you just redefine method again .Case 1 rule is applicable .

Thursday, April 14, 2011

Difference Between Java Bean and POJO


A POJO just means a "plain old java object".  A POJO can be *any* Java object.

A JavaBean is a Java object that satisfies certain programming conventions:
  • the JavaBean class must implement either Serializable or Externalizable
  • the JavaBean class must have a no-arg constructor
  • all JavaBean properties must public setter and getter methods (as appropriate)
  • all JavaBean instance variables should be private

So... all JavaBeans are POJOs but not all POJOs are JavaBeans.  A JavaBean is a POJO that follows certain coding conventions.

Tuesday, February 22, 2011

Message-Driven bean Vs Session bean

Message-Driven bean is integrated with Java Message Service (JMS) to provide the ability to act as message consumer and performs asynchronous processing between server and the message producer.

Session bean is a non-persistent object that implements some business logic running on server. There are two types of session Beans.
1. Steless Session Bean (each session bean can be used by multiple EJB clients)
2. Stateful Session Bean (each session is associated with one EJB client)

Sunday, January 23, 2011

What is OO ? actual

http://www.csis.pace.edu/~bergin/patterns/ppoop.html

Sunday, January 16, 2011

ceilingKey

1- public K ceilingKey(K key) {}
2-return key .
3-  Gets the entry corresponding to the specified key;
4-if no such entry exists, returns the entry for the least key greater than the specified key;
5-if no such entry exists (i.e., the greatest key in the Tree is less than the specified key), returns null

Example:
package com.example;

import java.util.TreeMap;

public class Test2 {
    public static void main(String[] args) {
        TreeMap treeExample=new TreeMap();
        treeExample.put(1, "AAA");
        treeExample.put(2, "BBB");
        treeExample.put(4, "DDD");
        System.out.println(treeExample.ceilingKey(1));
        System.out.println(treeExample.ceilingKey(3));
        System.out.println(treeExample.ceilingKey(6));
       
    }
}
 


Out Put:
1
4
null.
       

higherKey

   1- public K higherKey(K key) {}
   2-the method should return higher key 
   2-it is method which return the key .and returning key should be satisfy one within   following three condition
      1- if the higher key exist then it return higher key .
      2-if the higher key does not exist then returns the entry for the least  key greater than the specified key
          example :key 3 if 4 is not exist but 5 is there it's return 5
      3-if it not satisfy upper two condition then it should be return null

Example:
package com.example;

import java.util.TreeMap;

public class Test1 {

    public static void main(String[] args) {
        TreeMap treeExample=new TreeMap();
        treeExample.put(1, "AAA");
        treeExample.put(2, "BBB");
        treeExample.put(3, "CCC");
        treeExample.put(4, "DDD");
        treeExample.put(6, "FFF");
        System.out.println(treeExample.higherKey(3));
        System.out.println(treeExample.higherKey(4));
        System.out.println(treeExample.higherKey(6));
       
       
       
    }
   
   
}


Out Put:

4
6
null

subMap

1- SortedMap subMap(K fromKey, K toKey);
2-   public NavigableMap subMap(K fromKey, boolean fromInclusive,K toKey,   boolean toInclusive) {}
------------------------------------------------------------------------------------------------------------
   1- SortedMap subMap(K fromKey, K toKey);
1-Returns a view of the portion of this map whose keys range from fromKey  inclusive, to toKey , exclusive.
2-If fromKey and toKey are equal, the returned map is empty. 
3-The returned map is backed by this map, so changes
------------------------------------------------------------------------------------------------------------
2-   public NavigableMap subMap(K fromKey, boolean fromInclusive,K toKey,   boolean toInclusive) {}
This method is same as upper method but one main difference is that it provide extra facility to inclusive  and exclusive key as user demand
 ---------------------------------------------------------------------------------------------------------------------------------

Example:

import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;

public class Test {

    public static void main(String[] args) {
        TreeMap   setmap=new TreeMap();
        setmap.put(1, "AAA");
        setmap.put(2, "BBB");
        setmap.put(3, "CCC");
        setmap.put(4, "DDD");
        setmap.put(5, "EEE");
        SortedMap< Integer, String>  simple=setmap.subMap(2, 4);
        System.out.println(simple);
        SortedMap< Integer, String>  simple1=setmap.subMap(2,false, 4,true);
        System.out.println(simple1);
       
       
       
    }
   
}


OUTPUT :
{2=BBB, 3=CCC}
{3=CCC, 4=DDD}

 

tailMap

  SortedMap tailMap(K fromKey);

1-It is method in the TreeMap .
2-Returns a view of the portion of this map whose keys are greater than or equal to fromKey .
3-This method returns the portion of TreeMap whose keys are grater than or equal to fromKey.

Example :
ublic static void main(String[] args) {
                TreeMap mapexample = new TreeMap();
                map.put(1, "AAA");
                map.put(2, "BBB");
                map.put(3, "CCC");
                map.put(4, "DDD");
                SortedMap smap1 = map.tailMap(3);
                System.out.println(smap1);
            }
   

OutPut:{ 3=CCC 4=DDD}

HashMap Class

HashMap:
  1- deleration of HashMap is following
     public class HashMap extends AbstractMap
     implements Map, Cloneable, Serializable {}
  2-The HashMap gives you an unsorted, unordered Map.
  3-HashMap allows one null key in a collection and multiple null values in a collection.
  4-This class makes no guarantees as to the order of the map
  5- Iteration over collection views requires time proportional  to the "capacity" of the HashMap instance (the number of  buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity  too high (or the load factor too low) if iteration performance is important.
  6- HashMap has two parameter that affect its performance
     1-initial capacity
     2-load factor
     initial capacity: capacity at the time the hash table is      created .
     Load Factor :if the Hash table  is full ,then load factor is      automatically increased the size of HashMap.
    

Map interface

1-An object that maps keys to values. 
2-A map cannot contain duplicate keys;
3-each key can map to at most one value.
4-both the key and the value are objects.
5-The Map interface is provide three collection view
  1-collections of value
  2-set of keys
  3-set of key -value mappings
6-Four main classes which are implemented the Map interface
  1-HashMap
  2-Hashtable
  3-LinkedHashMap
  4-TreeMap
7-All general-purpose map implementation classes should provide two "standard"   constructors:
   1-a void (no arguments) constructor which creates an empty map
   2-a constructor with a single argument of type Map,
   3-which creates a new map with the same key-value mappings as its argument.
8-method in this interface
   1-  boolean containsKey(Object key);
   2-boolean containsValue(Object value);
   3-V get(Object key);
   4-V put(K key, V value);
   5-V setValue(V value);
   6-V getValue();
   7-boolean equals(Object o);
   8-int hashCode();

Sunday, January 9, 2011

encapsulation and abstraction ?

Q1: encapsulation is a type of "information hiding" in which you hide data attributes of a class behind getter and setter methods; this allows you to control how data is stored and manipulated.  For example you could have a class that holds an IP address (for a networking application).  The class might expose the IP address as a String, such as "127.0.0.1" with methods like getIpAddr():String and setIpAddr(String) but internally the ipAddr field could be stored as a Java int.

Whereas, "abstraction" is a concept about modeling: to create an abstraction is to find the "minimal amount of information" about the thing being modeled to solve the requirements of that "thing". 

Thursday, January 6, 2011

Scanner

package com.example;

import java.util.Scanner;

public class Example {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
         String aa=in.nextLine();
         System.out.println(Integer.parseInt(aa));
    }
   
}