Tuesday, 20 January 2015

Difference between ArrayList and HashMap


Both ArrayList and HashMap belongs to different Collection class in java.Both of them are used to store objects but their internal working is totally different


.                                              ArrayList Vs HashMap





       ArrayList

         HashMap
 Implementation
 ArrayList implements List interface.

 HashMap implements Map interface.
 Storage of Objects
ArrayList only stores one object. 

 HashMap stores tow object one is key and other is value.
Equals and HashCode 


 ArrayList does not have to implements these methods.
 HashMap must implement equals and hashcode method properly.
Order of Insertion 

 ArrayList maintains order of insertion.
 HashMap does not maintains order of insertion.





Happy Reading.Study Well.

Sunday, 18 January 2015

Java Program to Find IP Address of the Machine


                    Java Program to find IP Address of the Machine



import java.net.InetAddress;
import java.net.UnknownHostException;


public class FindIpAddress {

  /**
   * @param args
   */
  public static void main(String[] args) {
    
     
              InetAddress myIp = null;
        try {
          myIp = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
          System.out.println("Exception caught ="+e.getMessage());
        }
              System.out.println("IP of my system is := "+myIp.getHostAddress());
       
  }

}













Enjoy Reading.

Java Program to Print Prime Numbers


                                  Java Program to Print Prime Numbers

This java program prints prime numbers between the input value provided by the user. The smallest prime number starts with 2.



1:  import java.util.Scanner;  
2:  public class Prime {  
3:       /**  
4:        * @param args  
5:        */  
6:       public static void main(String[] args) {  
7:            int iNumber,iRemainderValue;  
8:            //Scanner class used for I/O operations  
9:            Scanner scanner=new Scanner(System.in);  
10:            System.out.println("Enter the number till which you want to generate prime numbers:");  
11:            iNumber=scanner.nextInt();  
12:            //outer loop iterates till iNumber value   
13:            for(int i=2;i<iNumber;i++)  
14:            {  
15:                 iRemainderValue=0;  
16:                 //inner loop checks if value of i is divisible by j values and sets the   
17:                 //iRemainderValue value accordingly.  
18:                 for(int j=2;j<i;j++)  
19:                 {  
20:                      if(i%j==0)  
21:                           iRemainderValue=1;  
22:                 }  
23:                 if(iRemainderValue==0)  
24:                 {  
25:                      System.out.print(i +" ");  
26:                 }  
27:            }  
28:       }  
29:  }  


Enjoy Reading.

Difference between ArrayList and Vector


Difference between ArrayList and Vector comes under Core Java interview questions.It is the most basic questions asked to check the interviewee basic knowledge of Collection before going deep in Collection.


                                                         ArrayList Vs Vector




              ArrayList
                Vector
 Synchronization
 ArrayList in not synchronized means it is not suitable for multi-threaded environment.

 Vector is synchronized.
 Performance
 Arraylist are faster as it is not synchronized.

 Vector performance is slow as they are thread safe.
 Resize
 ArrayList grow half of its size when resized.

 Vector grow double of its size when resized.
 Iteration method
 ArrayList does not uses enumerator for iteration.

 Vector uses enumerator for  iteration.



Read carefully and understand the difference.
Enjoy Reading

Wednesday, 14 January 2015

Difference between HashMap and HashTable

Both HashTable and HashMap implements Map interface but there are some differences between these two.It is one of the most popular questions asked in interview.So prepare well.

                                                    HashMap Vs HashTable




 HashMap
  HashTable

Synchronization or thread-safe
HashMap is not synchronized.hence It is better for non-threaded applications.


 HashTable is synchronized.This makes it better for multi-threading applications.

Iteration methods

 HashMap uses Iterator interface for iteration.
 HashTable use enumeration interafce for iterating the values.

Null Values/Keys

 HashMap allows one null key and any number of null values.
 whereas HashTable does not alloe null key and null values.

Performance

 HashMap is faster and uses less memory as it is not synchronized.
 HashTable takes more memory and slower than HashMap in performance.


Fail Fast Iterator

The iterator in HashMap is fail fast.The enumeration in HashTable is not fail fast.


Make sure you study this well.Happy Reading... :)

Monday, 22 December 2014

Difference between String Literal and String Object in Java


This is most confusing topic in java and its not given clearly in many websites what is the exactly difference between String Literal and String Object.And its a very basic question asked in interviews.
So here is my explanation on this:-

String are nothing but a sequence of characters for ex:"Hello".In java programming language string are objects.

The JVM maintains a special part of memory called "String Literal pool" or "String constant pool" where all string gets stored.This is done to avoid unnecessary creation of same object again and again which helps in saving memory as well as time.

When you declare a String as

String strName1="Kavita";

Here JVM first checks whether a String named "Kavita" already present in String pool.If yes,variable 'strName' refers to the object "Kavita". If no,then it creates an object "Kavita" in String pool and variable 'strName' points to this object.
This declaration creates ONE object.

Similarly, when you create another object as

String strName2="Kavita";

In this case , JVM checks the string "Kavita" in String pool.As "Kavita" object is already present in String pool. JVM returns the reference of "Kavita" object to variable 'strName2'.So NO objects gets created.

And both variable 'strName1' and 'strName2' points to the same object "Kavita" present in string pool.This way JVM save the memory and time in creation of string objects.If you call '==' operator on both the variable,then it will return TRUE.

String objects gets stored in heap area of memory.String pool is located in premgen area of heap in Java version 1.6 or before but in Java 1.7 , it is moved to heap area of memory.

When you declare String using new operator as,

String strName3=new String("Rob");

Here ,'new String("Rob")' will always a create an object in heap and another object "Rob" will be created which gets stored in String pool.The reference of new String("Rob") will be returned to variable strName3. So in this case,Two objects gets created.


Happy Learning. :)

Sunday, 21 December 2014

Important Eclipse Keyboard Shortcuts For Java Programmer


Here I am listing some basic and important eclipse keyboard shortcuts that every programmer should use while developing any application

The benefit of using shortcuts is it helps you to develop your application faster.

The list of shortcuts are as follows:

Ctrl+N                                                
Create New Project
Ctrl+Shift+R                                      
Open Resource (Java files,other files,folder,projects)
Ctrl+W                                                
Close Current Opened File.
Ctrl+Shift+W                                      
Close All Files
F5                                                        
Refresh


Ctrl+C                                                
Copy Some text or line.
Ctrl+V                                                
Paste text or line.
Ctrl+X                                                
Cut text or line
Ctrl+Z                                                
Undo Last Action
Ctrl+Y                                                
Redo Last Action
Ctrl+F                                                  
Open find and replace dialog to find any word or strings of words.
Ctrl+H                                                
Search Entire Workspace for file,words etc
Ctrl+Shift+O                                      
Organize all imports and remove unnecessary imports
Ctrl+A                                                
Select all text in class
Ctrl+I
Correct indention of selected text or of current line or whole class
Ctrl+Shift+F
Format all code in class
Ctrl+/
Comment / un-comment line
Ctrl+Shift+/
Add Block Comment around selection
Ctrl+Shift+\
Remove Block Comment
Ctrl+S                                                  
Save Current File.
Ctrl+Shift+S                                        
Save All Files. 



Important Debug shortcuts :

F11
Debug                                                                       
F5
Step Inside the function
F6
Next step(line by line)
F7
Step out of the function
F8
Skip to next breakpoint