Equals and = = operator

Equals and = = operator -
= = is a operator, which is used for check the equality of two values.
if two values are equal then it will return true otherwise return false.

equals() is a method of object class, which is used for check the equality of two objects.
if two objects are equal then it will return true otherwise return false.     

public class Test {
public static void main(String[] args) {

int int1  = 10;
int int2  = 10;

Integer one = new Integer(int1);
Integer two = new Integer(int2);       

//compare for two objects
if(one.equals(two)){
System.out.println("two objects are equals");
}
// compare two values
if(int1 == int2){
System.out.println("Two values are equals");
}
}
}

output :
two objects are equals
Two values are equals

Deep Knowledge about this -

in the above figure, I am trying to make a sense about the example - 
when we make first int variable

int int1=10;
value 10 is set in the memory and point to the int1 variable, again

int int2=10;
value 10 is point to int2 variable.

Integer one = new Integer(int1);
in this case value 10 will be wrapped by Integer object and assigned to “one” variable.

Integer two = new Integer(int2);   
in this case value 10 will be wrapped by Integer object and assigned to “two” variable.

In the all case, notice that the value 10 is occurs in a single time in memory but above two cases it is assigned into int type and wrapped by Integer class.

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

Shallow Copy Deep Copy Java example