Thursday, November 19, 2015

Difference between equal method and ==


Java == check if bot the object are at the same memory location in heap.
Where as java .equal method compares the content of the object


 public class Class2 {  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           new Class2().testEqual();  
      }  
      public void testEqual() {  
           List<String> list1 = Arrays.asList("Apple", "Banana", "Oranges");  
           List<String> list2 = Arrays.asList("Apple", "Banana", "Oranges");  
           List<String> list3 = list1;  
           if (list1 == list2) {  
                System.out.println("true");  
           }  
           else if (list1 == list3) {  
 System.out.println("list1 == list3");  
           }   
           else if (list1.equals(list2))               ;  
           {  
                System.out.println("equal");  
           }  
      }  
 }  


OUTPUT

list1 == list3
equal

No comments:

Post a Comment