wellnwill Marathahalli Bangalore

Equality Operator

  • We can apply equality operators for every primitive types including boolean type also.
  • Equality operators can be applied for reference types also but they compares references (address) not values.
Operator Result
== Equal to
!= Not equal to
class Test45
{
  public static void main(String argss[])
  {
    System.out.println(true==true); 
    System.out.println(true==false);
  }
}

// O/P - true,false







class Test
{
  int id=10;
  public static void main(String argss[])
  {
    Test t1=20; // CompileTimeException
    Test t1=new Test();
    Test t2=new Test();
    System.out.println(t1.id==t2.id); // true
    System.out.println(t1);
    System.out.println(t2);
    System.out.println(t1==t2);  // false
    System.out.println(true==true);  // true
    System.out.println(true==false); // false
    System.out.println(2==2);//true
    }
}
class Car
{
}

class Bike
{
  public static void main(String argss[])
  {
     Car c=new Car();
     Bike b=new Bike();
     System.out.println(c==tb);   
  }
}

// O/P - CompieTimeException
class Vehicle
{
}
class Bike extends Vehicle
{
  public static void main(String args[])
 {
   Vehicle v1=new Vehicle();
   Bike b1=new Bike();
   System.out.println(v1==b1);
 }
}

// O/P - false
Note-because of relation between the classes, we can compare the object reference through relational operator.
Footer with Map