Tuesday, November 6, 2012

If two classes are within the same package you dont have to use import statement

(a)
package pack1;
public class ClassA extends ClassB {

    public static void main(String[] args) {
        /*
          You can see from here when we are importing a class within the same package
          say pack1 you need not explicitly import the package say import pack1.ClassB
          also in the class A if you see we have not given any declaration for the integer
          variable i (without any declaration it will take it as default or else protected or public)
        */
        ClassB obj = new ClassB();
        System.out.println("The Output in Class A is " + obj.i);

    }

}

(b)
package pack1;
public class ClassB {
    //Note : If you declare it as private it will show error in ClassA as the classes are not same
    //Can be declared protected, public or (just like that..that is default)
    int i=10;
}

Answer
----------
The Output in Class A is 10

How to use size method in java

import java.util.*;
public class size {

    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        a1.add("1");
        a1.add("2");
        a1.add("3");
        System.out.println("\n ArrayList size is "+a1.size());
    }

}

Answer
----------
ArrayList size is 3

retainAll method of Collection interface in java

import java.util.*;
public class retainAll {
    public static void main(String[] args) {


        ArrayList a1 = new ArrayList();
        a1.add(1);
        a1.add(2);
       
        ArrayList a2 = new ArrayList();
        a2.add(3);
        a2.add(4);       
       
        a2.add(a1);
        for(int i=0;i<a2.size();i++)
        {
            System.out.println("a2 list is " + a2.get(i));
        }       
        System.out.println("");
        boolean retainCheck;
       
        retainCheck = a2.retainAll(a1);
        /*
        a2.retainAll(a1) will delete all the elements of the invoked collection (a1) and will retain 
        all the elements of the invoking collection a2
        */
       
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("a1 list is " + a1.get(i));
        }
        System.out.println("");
        System.out.println("a2's size is " + a2.size());
        for(int i=0;i<a2.size();i++)
        {
            System.out.println("a2 list is " + a2.get(i));
        }       
       
    }
}

Answer
----------
a2 list is 3
a2 list is 4
a2 list is [1, 2]

a1 list is 1
a1 list is 2

a2's size is 0

How to use iterator method in java

/*
iterator() method is defined as  - Iterator iterator in Collection interface
*/
import java.util.*;
public class iteratorMethod {
    public static void main(String[] args) {


        ArrayList a1 = new ArrayList();
        a1.add("a");
        a1.add("b");
        a1.add("c");
        a1.add("d");
       
        Iterator itr = a1.iterator();
        while(itr.hasNext())
        {
            Object instance = itr.next();
            System.out.println("The Output is " + instance);
        }
       
    }
}

Answer
----------
The Output is a
The Output is b
The Output is c
The Output is d

isEmpty method of Collection interface in java

/*
   isEmpty() method is defined as - boolean isEmpty() in Collection interface
*/
import java.util.*;
public class isEmpty {

    public static void main(String[] args) {
       
        ArrayList a1 = new ArrayList();
        a1.add("One");
        a1.add("Two");
        a1.add("Three");   
       
        boolean removeCheck;
        removeCheck = a1.remove(a1);
        removeCheck = a1.remove(a1);
        //.....
       
        System.out.println("The ArrayList on performing the a1.remove(a1) method) " + a1.isEmpty());
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The ArrayList elements (after the remove() method) are " + a1.get(i));
        }
        /*
        because a1.remove(a1) tries to remove the entire elements present in a1 but since boolean 
        remove(Object obj) is intended to remove only one instance object from the invoking  
        collection something like this...below would be the correct code
        */
        removeCheck = a1.remove(a1.get(0));
        removeCheck = a1.remove(a1.get(1));
        for(int i=0;i<a1.size();i++)
        {       
            System.out.println("\n The ArrayList will now decrease (after performing the remove(a1.get(i)) method) " + a1.get(i) + "\n");
        }
       
        a1.add("Four");
        a1.add("Five");
        a1.add("Six");       
        removeCheck = a1.removeAll(a1);
        System.out.println("The ArrayList should be empty (after removeAll() method) " + a1.isEmpty() + " as the size is now " +a1.size() + "\n");
       
        //The below does not gets displayed because the
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The ArrayList elements (after the removeAll() method) are" + a1.get(i));
        }       
               
    }

}

Answer
--------
The ArrayList on performing the a1.remove(a1) method) false
The ArrayList elements (after the remove() method) are One
The ArrayList elements (after the remove() method) are Two
The ArrayList elements (after the remove() method) are Three

The ArrayList will now decrease (after performing the remove(a1.get(i)) method) Two

The ArrayList should be empty (after removeAll() method) true as the size is now 0

indexOf method in List Interface in Java

/*
indexOf method is defined as  - int indexOf(Object obj) in List Interface
*/

import java.util.*;
public class indexOf {

    public static void main(String[] args) {
       
        ArrayList a1 = new ArrayList();
        a1.add(0,"a");
        a1.add(1,"b");
        a1.add(2,"c");
        a1.add(3,"d");
       
        System.out.println("The size of a1 is " + a1.size());
        System.out.println("The Value of a1 is " + a1.indexOf("c"));
    }

}

Answer
----------
The size of a1 is 4
The Value of a1 is 2

hashCode in Java

import java.util.ArrayList;
public class hashCode {

    public static void main(String[] args) {

        ArrayList a1 = new ArrayList();
        a1.add("One");
        a1.add("Two");
        a1.add("Three");
       
        int hashCodeforInvokingCollection = 0;
        for(int i=0;i<a1.size();i++)
        {
            //hashCodeforInvokingCollection = a1.get(i).hashCode();
            hashCodeforInvokingCollection = a1.get(i).hashCode();
            System.out.println(" a1's "+a1.get(i)+" " +hashCodeforInvokingCollection);
        }       
       
    }
}

Answer
----------
a1's One 79430
a1's Two 84524
a1's Three 80786814