X
    Categories: java

Java Relational Operators || Java example

Java Relational Operators :

Relationship operators are used to compare variables to find whether they are equal, greater than or less than and few more operations.

We mostly use these operators to perform conditions in the java programming let’s get into for more details.

  1. Equal to Operator (==)
  2. Not equal to Operator (!=)
  3. Greater than Operator (>)
  4. Less than Operator (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

 

They are used in different scenarios depending upon the requirement

Equal to Operator (==):

When we want to compare two variables i.e., integers or String we can use this operator

Using Strings :

public static String a;
public static String b;

public static void main(String args[]) {

    a = "Abhi";
    b = "Abhi";

    System.out.println(a == b);

}

 

also we can use equals to as below

System.out.println(a.equals(b));

 

 

Using int:

public static int a;
public static int b;

public static void main(String args[]) {

    a = 10;
    b = 10;

    System.out.println(a == b);

}

 

 

Not equals to operator (!=):

public static void main(String args[]) {

    a = 10;
    b = 10;

    System.out.println(a != b);

}

 

 

Greater than (>):

public static int a;
public static int b;

public static void main(String args[]) {

    a = 15;
    b = 10;

    System.out.println(a > b);

}

 

 

Less than (<)

public static int a;
public static int b;

public static void main(String args[]) {

    a = 25;
    b = 20;

    System.out.println(a < b);

}

 

Greater than or Equal to (>=):

public static int a;
public static int b;
public static int c;
public static int d;

public static void main(String args[]) {

    a = 35;
    b = 30;
    c = 40;
    d = 30;

    System.out.println(a >= b);
    System.out.println(b >= c);
    System.out.println(b >= d); // here b is equal to d

}

 

 

Less than or equal to (<=):

public static int a;
public static int b;
public static int c;
public static int d;

public static void main(String args[]) {

    a = 2;
    b = 10;
    c = 8;
    d = 10;

    System.out.println(a <= b);
    System.out.println(b <= c);
    System.out.println(b <= d); // here b is equal to d
}

 

 

Any queries on Java Relational Operators let us know in the comment section below.

Do like and share for more interesting tutorials.

 

abhishek:
Related Post

This website uses cookies.