Total members 11890 |It is currently Sat Apr 20, 2024 10:58 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Equality in java :
Today, we talk about equality in java which means how to check if two variables are equal or in other means having the same values. To illustrate equality concept in java we have to partition our thinking into three sections. First, equality for varirables of primitives data types such as integers,floats,byte,double,long and boolean. Second is the equality of reference variables. Finally, is checking if two objects are equal in java.

Primitive variables equality


Two equal primitive variables are equal when they carry the same value. To check this you can use (==) operator.

Two Integers equality :
java code
int x=5; 
int y=5;
if(x==y)
{
System.out.println("x is equal y");
}

Float equality:
java code
float xFloat = 5.1f;
float yFloat = 5.1f;
if (xFloat == yFloat) {
System.out.println("xFloat is equal yFloat");
}

Boolean equality
java code
boolean xBoolean = true;
boolean yBoolean = true;
if (xBoolean == yBoolean) {
System.out.println("xBoolean is equal yBoolean");
}

Using equality operator you can have more than two operands by adding logical operators such as "&" for AND :
java code
int x = 5;
int y = 5;
int z = 4;
int u = 5;
if ((x == y)&&(z == u) && (x == z)) {
System.out.println("They are equal");
}else
{
System.out.println("They are not equal");
}

Using not logical operator equal :
java code
int x = 5;
int y = 3;
if (x!=y) {
System.out.println("They are not equal");
}


Reference variables equality


Two reference are equal when they refer to the same object at the memory. Using the == operator you can't compare the content of objects. Following example contains two classes ("class1",and "class2") and two reference variables "obj1Ref" and "obj2Ref". References equality is applied:
java code
public class Main {

public static void main(String[] args) {

class1 obj1Ref=new class1();
class1 obj2Ref=new class1();

if(obj1Ref==obj2Ref)
{
System.out.println("# obj1Ref equals obj2Ref ");
}else
{
System.out.println("# obj1Ref not equal to obj2Ref ");
}
// to be looking at the same object
obj1Ref=obj2Ref;
if(obj1Ref==obj2Ref)
{
System.out.println("# obj1Ref equals obj2Ref ");
}
}
}
class class1{
}
class class2{
}

The output of equality conditions is :
Code:
# obj1Ref not equal to obj2Ref
# obj1Ref equals obj2Ref

Note that when you use (==) operator between two object references they must be from the same class type. The following example will not compile:
java code
class1 obj1Ref=new class1();
class2 obj2Ref=new class2();

if(obj1Ref==obj2Ref)
{
System.out.println("# obj1Ref equals obj2Ref ");
}

You will get compile error at if(obj1Ref==obj2Ref).


Objects equality


All the classes in java inherit by default from the class Object. The class Object has a group of functions that is common between all java classes such as clone(), toString() and equals(). You should use equals() function when it to comes to case you want to check the similarity of two objects specially that you can override the equals() function in your own class and set the similarity conditions you want.

Example :
Code:
   myObject.equals(otherObject);

Some java classes already override this function such as String class :
java code
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}


The usage of equals function for String class is as follows :
java code
String st1="String1";
String st2="String1";
if(st1.equals(st2))
{
System.out.println("The String values are equal");
}

As equals() function is overridden at String class you can customize overriding for any newly created class such as "class1" below:
java code
public class Main {
public static void main(String[] args) {

class1 ref1=new class1(4,3);
class1 ref2=new class1(4,55);
if(ref1.equals(ref2))
{
System.out.println("The two object are equal");
}
if(ref1!=ref2)
{
System.out.println("The two reference are different");
}
}
}
class class1{
private int x;
private int y;

public class1(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
@Override
public boolean equals(Object obj) {
return this.x== ((class1)obj).getX();
}

Notice that the two objects instances are considered equal but the two variables references are not( using == only)
The output of this snippet is :
java code
The two object are equal
The two reference are different

Comparing two objects using CompareTo() function:
Most of java classes such as Date,String,Float,Integer implements Comparable interface, that is why they can be sorted normally using Arrays.sort(),Collections.sort() functions. CompareTo() function can be implemented for any java classes. This function could return three values (1,0,-1). 1 means larger than the current object, 0 means equal the current object, -1 means less than current object. While we are talking about Java equality It is important to take a look at CompareTo() usage example, follow is class1() implements Comparable interface. Six objects are created from class1 and sorted using built in sorting function using Collections.sort().
java code
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

public static void main(String[] args) {

class1 ref1=new class1(4,3);
class1 ref2=new class1(4,55);
class1 ref3=new class1(53,12);
class1 ref4=new class1(43,52);
class1 ref5=new class1(44,44);
class1 ref6=new class1(11,11);
List<class1> arrList =new ArrayList();
arrList.add(ref1);
arrList.add(ref2);
arrList.add(ref3);
arrList.add(ref4);
arrList.add(ref5);
arrList.add(ref6);
Collections.sort(arrList);
for(int i=0;i<arrList.size();i++)
{
System.out.println(" : "+arrList.get(i).getX()+" "+arrList.get(i).getY());
}

}
}
class class1 implements Comparable {
private int x;
private int y;
public class1(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
@Override
public boolean equals(Object obj) {
return this.x== ((class1)obj).getX();
}
@Override
public int compareTo(Object o) {
if(this.x<((class1)o).getX())
{
return 1;
}else if(this.x>((class1)o).getX())
{
return -1;
}else
{
return 0;
}
}
public int getY() {
return y;
}
}


The output of this snippet is :
Code:
: 53  12
  : 44  44
  : 43  52
  : 11  11
  : 4  3
  : 4  55




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Equality in java
 Equality Operators     -  
 String compare equality     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Java course     -  
 What is Java API?!!!     -  
 java or .net     -  
 need help in java     -  
 Using FTP in java     -  
 Java Microprocessor     -  



Topic Tags

Java Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com