Tuesday, 17 October 2017

Can we overload main() in java?


Yes, the main() method in java can be overloaded.

Given below is the sample code snippet where main() method has been overloaded twice. If you want you can experiment by overloading main() as per your choice and requirement.

To prove our point that main() can be overloaded, below code is sufficient. Have a look,

Source FileMainOverloading.java 

package basic;

public class MainOverloading {

      public static void main(String[] args) {
            System.out.println("Overloaded methods will not be executed here");

      }
     
      public static void main(int x){
            System.out.println("x="+ x);
            main(5,54);
      }
     
      public static void main(int a, int b){
            System.out.println("a="+ a);
            System.out.println("b="+ b);
      }

}


OUTPUT:
Overloaded methods will not be executed here 

---------------------------------------------------------------------------------------------------

In the above code snippet JVM searches for public static void main(String[] args) type signature as regular. And anything inside public static void main(String[] args)  will be executed first. So if you want your overloaded main() method to get executed, you need to call it from inside actual main() which is entry point for all java program. 

Given below is modified version of above code snippet. Here overloaded main() is executed. Have a look,
---------------------------------------------------------------------------------------------------

Source FileMainOverloading.java 

package basic;

public class MainOverloading {

      public static void main(String[] args) {
        main(1);       //calling overloaded main

      }
     
      public static void main(int x){
            System.out.println("x="x);
            main(5,54);
      }
     
      public static void main(int aint b){
            System.out.println("a="a);
            System.out.println("b="b);
      }

}


OUTPUT:
x=1    //output of main(int x)
a=5    //output of main(int aint b) 
b=54

Sunday, 15 October 2017

Does java support pointer?

Java doesn't support pointer directly.

Reason: Improper usage and handling of pointer may lead to memory leakage along with reliability issue.

Apart from this pointer can be used to access the memory area directly and thus could help users with malicious intent in altering sensitive data stored at the memory location in illegal way. This could in turn pose serious security issue. But java is secured language because it does not using pointer.

And most probably this is the reason why java makers decided to omit pointer from java.

Saturday, 16 September 2017

TreeSet in java


TreeSet 
is a class that implements Set interface and extends AbstractSet class. By default, elements in TreeSet class is stored in ascending order (see example given below).


Key feature of TreeSet

1.     TreeSet contains only unique elements.
2.     Maintains ascending order (by default). 
3.     Access and retrival time is fast in TreeSet.


Now let’s look at the ways in which you can create TreeSet in java,
  • ·        TreeSet <E> mySet = new TreeSet<E>();     //<E> denotes generics
  • ·        TreeSet <E> mySet = new TreeSet<E>( Collection c);     //<E> denotes generics


Simple Example:

package javaRadarTreeSet;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

      public static void main(String[] args) {
            //Create treeset
            TreeSet<String> treeSet = new TreeSet<String>();
            //Add elements to treeset
            treeSet.add("Omjit");
            treeSet.add("Krishna");
            treeSet.add("Mithilesh");
            treeSet.add("Manish");
            treeSet.add("Puja");
            treeSet.add("Shweta");
            treeSet.add("Rita");

            //Traverse the elements of treeset
            Iterator<String> setIterator = treeSet.iterator();
            while(setIterator.hasNext()){
                  String element = setIterator.next();
                  System.out.println(element);
            }

      }

}
OUTPUT:
Krishna
Manish
Mithilesh
Omjit
Puja
Rita
Shweta

Now let’s look at different ways in which you can use the TreeSet that you created. 
Click on each link to read about them.

TreeSet operations

·         Sort the elements in TreeSet  --> (by default its sorted in ascending order)
·         Search element in TreeSet
·         Remove element from a particular index in TreeSet

Traverse TreeSet

·         Traverse TreeSet using Iterator
·         Traverse TreeSet using ListIterator
·         Traverse TreeSet using Enumeration

Thursday, 17 August 2017

What is the advantage of iterating Collection using iterator?

We can use for loop, advanced for loop or iterator for iterating over Collection.

But using iterator over other competitors gives some advantages like:

  • By using for loop you cannot update(add/remove) the Collection whereas with iterator we can easily update Collection.
Example for updating list by iterator:

package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class UpdateList {

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

            System.out.println("Original list:" +javaRadarList);
           
            Iterator<String> itr = javaRadarList.iterator();
            while(itr.hasNext()){
                  String token = itr.next();
                  itr.remove();
                  System.out.println("list afer modification:" +javaRadarList);
            }
                 
      }

}

OUTPUT:
Original list:[Java, Spring, Hibernate, EJB]
list afer modification:[Spring, Hibernate, EJB]
list afer modification:[Hibernate, EJB]
list afer modification:[EJB]
list afer modification:[]


NOTE: Try to remove object from Collection using for loop and see if you can or not.

  • In situations where there is no idea of what type of Collection will be used we can use iterator to traverse over the collection as all Collection implements iterator interface.

Sunday, 13 August 2017

Difference: HashMap vs HashTable


HashMap and HashTable differ based on the following parameters:

  • Nature
           HashMap is not synchronized in nature.
           HashTable is synchronized in nature

  • Traversed by
           HashMap can be traversed by using an iterator
           HashTable can be traversed by using an iterator or enumerator

  • Null value/key
          HashMap allows only one null key and any number of null values.
          HashTable doesn't allow key or value as null.

  • fail-fast nature of iterator
          Iterator in HashMap is fail-fast.
          Enumerator in HashTable is not fail-fast.

  • Performance
          HashMap is faster performance wise.
          HashTable is slower in comparision to HashMap performance wise.
          
          Reason: Since HashTable is synchronized in nature so it doesn't allow multi-threading                                       operation on a particular resource at a time thus turning out to be slower than                                       HashMap which is non-synchronized by nature.



You may also like to read:



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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.