Difference between “=” and “==” in Java

 

One liner answer to this question would be:
“=” operator is for assigning a value to a variable whereas “==” operator compares the objects’ location(s) with respect to memory.
 
Thus,
“=” is an simple Assignment Operator which assigns values of variable on it’s right side variable to the left side operand.
“==” is a Relational Operator which checks if the values of two variables are equal or not. If yes then condition returns true otherwise, it returns false.
 
For example, below code explain the operators on primitive variables like int, float etc.
Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java.
 
publicvoidtestMain(Object[] args){

                inta = 5;
                int b = 5;
                intc = a+b;

                System.out.println("c=" + c); //This statement would print 10
                intd = a;
                System.out.println("d=" + d); //This statement would print 5
                inte = c;
                System.out.println("e=" + e); //This statement would print 10
                if(a==b)
                                System.out.println("True");
                else
                                System.out.println("False");
                //The above would print True: a=b=5

                if(a==c)
                                System.out.println("True");
                else
                                System.out.println("False");
                //The above would print False: a=5 & c=10

                if(a==d)
                                System.out.println("True");
                else
                                System.out.println("False");       
                //The above would print True; a=5, d=a=5(as initialized above), thus a=d

}

Thus, output of above piece of code is:

c=10

d=5

e=10

True

False

True

Let’s learn some basics on ‘char’ first before proceeding to the example with ‘char’ variables. ‘char’ is a primitive type, and it can hold a single character or a Unicode/ASCII value.

public void testMain(Object[] args){

                chara = '5';
                System.out.println("Value of variable a=" + a);
                System.out.println("ASCII value of variable a=" + (int) a);

                charb = '5';
                System.out.println("nValue of variable b=" + b);
                System.out.println("ASCII value of b=" + (int) b);

                charc = 'j';
                System.out.println("nValue of variable c=" + c);
                System.out.println("ASCII value of c=" + (int) c);

                chard = (char) (a+b); //first adds the ASCII values of variables a and b which is 53+53=106; and then convert that ASCII value to it's corresponding character which is 'j'.

                System.out.println("nValue of variable d=" + d);
                System.out.println("ASCII value of d=" + (int) d);

                chare = a;
                System.out.println("nValue of variable e=" + e); //This statement would print 5

                if(a==b)
                                System.out.println("True");
                else
                                System.out.println("False");

                //The above would print True: a=b='5'

                if(a==c)
                                System.out.println("True");
                else
                                System.out.println("False");

                //The above would print False: a='5' & c='j'

                if(a==e)
                                System.out.println("True");
                else
                                System.out.println("False");                 

                //The above would print True; a='5', e=a='5'(as initialized above), thus a=e

}

Here is the output of above piece of code:

Value of variable a=5

ASCII value of variable a=53

Value of variable b=5

ASCII value of b=53

Value of variable c=j

ASCII value of c=106

Value of variable d=j

ASCII value of d=106

Value of variable e=5

True

False

True

Below code explains the operators on reference variables like string.

public void testMain(Object[] args){

                String a = "Smile"; //This statement creates a String Literal which points to a memory location in the String pool (which is shared) if it already exists. If it does not exists, it createsanew.

                String b = new String("Smile"); //This statement creates a String Object in the heap memory which is not shared.

                if(a==b)
                                System.out.println("True");

                else
                                System.out.println("False");

                //this prints "False" because variable a and b are pointing to different memory locations.

                String c = "Smile"; //This statement would store variable c in the same memory location as variable a

                if(a==c)
                                System.out.println("True");

                else
                                System.out.println("False");

                //Since both a and c points to same memory location, thus having same data, it prints True

                if(c=="5")
                                System.out.println("True");

                else
                                System.out.println("False");

                //This comparisioncreates a new memory location for undeclared data "5". Thus it would print false                                                
}

Below is the output of above piece of code

False

True

False

Thus, the statement we gave in the beginning is actually correct but incomplete. The key to understanding here is the last word ‘memory’.

So, the complete one line answer to the question would be:

“=” operator is for assigning a value to a variable whereas “==” operator compares the objects’ location(s) in memory (Not the value).

Read more on JAVA Code – HERE

This Post Has 2 Comments

  1. Saumy Nagayach

    Woah! This reminded me of the college days. I always had some fascination with these operators. I don't use them often now as am a tester by profession but there was a time when I used to mess up with this operator in specific, a lot! 🙂

  2. Amit

    Same as in C.

Comments are closed.