Wednesday, 31 May 2017

What is marker Interface in java?

Marker Interface doesn't have any method or field or constant declared within it. So a marker interface is an empty interface.

Example :

  • Serializable
  • Cloneable, 
  • RandomAccess interface and 
  • Remote interface


Now one question that would immediately pop-up in your mind is,

Why would one declare an empty interface? 
What good is it of?

In general a common belief is that a marker interface is generally used to send some kind of signal to the JVM and instructs JVM to  treat the class implementing it in a special way.

Like, when a class implements Cloneable marker interface, it signals the compiler that the class allows creation of clones of its objects.

Generally, a marker interface is used with the instanceof operator to check if a reference type variable refers to an object whose class implements the marker interface.


You may also like to read about:



Traverse ArrayList using Enumeration

package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.ListIterator;

public class EnumerationTraversal {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            //creating enumeration to traverse over javaRadarList
            Enumeration<String> listEnumerator = Collections.enumeration(javaRadarList);

            System.out.println("List traversal using Enumeration");
            while(listEnumerator.hasMoreElements()){
                  System.out.println(listEnumerator.nextElement());
            }
      }

}

OUTPUT:
List traversal using Enumeration
Java
Jquery
Spring
Hibernate
EJB


Tuesday, 30 May 2017

Remove element from array list

package javaRadarArrayList;

import java.util.ArrayList;

public class RemoveAtIndex {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");
           
            System.out.println("Before Removal");
            for(String list : javaRadarList){
                  System.out.println(list);
            }
           
            //Remove element from 2nd position
            javaRadarList.remove(1);
           
            System.out.println("After Removal");
            for(String list : javaRadarList){
                  System.out.println(list);
            }

      }

}

OUTPUT:

 Before Removal
Java
Jquery
Spring
Hibernate
EJB
After Removal
Java
Spring
Hibernate
EJB

Compare the result and you will find that element at 2nd position(Jquery) will not appear in After Removal result.

Remove element from linked list

package javaRadarLinkedList;

import java.util.LinkedList;

public class RemoveAtIndex {

      public static void main(String[] args) {
            //create Array List
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");
           
            System.out.println("Before Removal");
            for(String list : javaRadarList){
                  System.out.println(list);
            }
           
            //Remove element from 2nd position
            javaRadarList.remove(1);
           
            System.out.println("After Removal");
            for(String list : javaRadarList){
                  System.out.println(list);
            }

      }

}


OUTPUT:

Before Removal
Java
Jquery
Spring
Hibernate
EJB
After Removal
Java
Spring
Hibernate
EJB

Monday, 29 May 2017

Traverse Linked list using iterator

package javaRadarLinkedList;

import java.util.Iterator;
import java.util.LinkedList;

public class ItrTraversal {

      public static void main(String[] args) {
            //create Linked List
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to LinkedList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            //creating iterator to traverse over javaRadarList
            Iterator<String> listIterator = javaRadarList.iterator();

            System.out.println("Traversal in forward direction");
            while(listIterator.hasNext()){
                  System.out.println(listIterator.next());
            }
      }

}

OUTPUT:
Traversal in forward direction
Java
Jquery
Spring
Hibernate
EJB 


Traverse linked list using enumeration

package javaRadarLinkedList;

import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;

public class EnumerationTraversal {

      public static void main(String[] args) {
            //create Linked List
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to LinkedList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            //creating iterator to traverse over javaRadarList
            Enumeration<String> listEnumerator = Collections.enumeration(javaRadarList);

            System.out.println("List traversal using Enumeration");
            while(listEnumerator.hasMoreElements()){
                  System.out.println(listEnumerator.nextElement());
            }
      }

}


Traverse linked list using ListIterator

package javaRadarLinkedList;

import java.util.LinkedList;
import java.util.ListIterator;

public class ListItrTraversal {

      public static void main(String[] args) {
            //create Linked List
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to LinkedList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            //creating list iterator to traverse over javaRadarList
            ListIterator<String> listIterator = javaRadarList.listIterator();
           
            System.out.println("Traversal in forward direction");
            while(listIterator.hasNext()){
                  System.out.println(listIterator.next());
            }
           
            System.out.println("Traversal in backward direction");
            while(listIterator.hasPrevious()){
                  System.out.println(listIterator.previous());
            }
      }

}


 OUTPUT:
Traversal in forward direction
Java
Jquery
Spring
Hibernate
EJB
Traversal in backward direction
EJB
Hibernate
Spring
Jquery
Java
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.