• Home
  • What does == mean in Java?

What does == mean in Java?

In Java, the “==” operator is used to compare the values of two variables. It returns a boolean value of true if the values of the variables are equal, and false if they are not equal.

For example:

int a = 5;
int b = 6;

if (a == b) {
System.out.println(“a and b are equal”);
} else {
System.out.println(“a and b are not equal”);
}

This code will print “a and b are not equal” because the value of a is not equal to the value of b.

It’s important to note that the “==” operator is used to compare the values of two variables, not the references to the objects they may contain. To compare the references, you can use the “===” operator in JavaScript or the “equals()” method in Java.

For example:

Object obj1 = new Object();
Object obj2 = obj1;

if (obj1 == obj2) {
System.out.println(“obj1 and obj2 are equal”);
} else {
System.out.println(“obj1 and obj2 are not equal”);
}

This code will print “obj1 and obj2 are equal” because obj1 and obj2 both refer to the same object.