java operators tutorial for beginners

The JAVA operators belong to many categories: (h1)

  1. ·        Assignment JAVA operators (=, +=,-=,*=,-=)
  2. ·        Arithmetic JAVA operators (+,-, /, %)
  3. ·        Relational JAVA operators (==, >, >=,<,<=, !=)
  4. ·        Logical JAVA operators (&&, ||, !)
  5. ·        Bitwise JAVA Operators (Advanced topic: it will be dealt with in this tutorial)

To learn how to use the assignment and Arithmetic JAVA operators by examples, please check this tutorial.

The Relational JAVA Operators:

These operators are generally used in the conditions to test if a condition is true or not. The subsequent treatments will be based on the result of the evaluation of that condition.

To know how to express a JAVA condition, please check the correspondent tutorial.

The “==” JAVA operator: this JAVA operator is used to test if two expressions are equal: if the two expressions are equal, the condition is evaluated to true and otherwise it is evaluated to false.

The condition is written in this way: <expression1> == <expression2>

Expression may be a numeric value or a math expression.

Remark: you should not confuse between the two JAVA operators: the assignment operator (single equal sign) “=” and the equality test operator “==” (double equal sign)

int a = 5;
int b = 6;
int c = 2 + 3;
Boolean notEqual = (a==b);
Boolean equal = (a== c);
System.out.println(notEqual);
System.out.println(equal);

The variables of type Boolean (equal and notEqual) hold the result of the equal test. The notEqual should be equal to false and the equal variable should be equal to true.

The “<” JAVA operator: this JAVA operator is the “less than” operator. It is used to test if an expression is less than another expression.

The condition is written in this way: <expression1> < <expression2>

Expression may be a numeric value or a math expression.

Remark: the condition using this operator “<” is evaluated to false, if <condition 1> is equal to <condition 2>

int a = 5;
int b = 6;
int c = 2 + 3;
Boolean b1 = (a < b);
Boolean b2 = (a < c);
System.out.println(b1);
System.out.println(b2);

b1 is equal to true because a < b (5<6).

b2 is equal to false because a isn’t less than c (a= c = 5)

The “<=” JAVA operator: this JAVA operator is the “less or equal” operator. It is used to test if an expression is less or equal to another expression.

The condition is written in this way: <expression1> <= <expression2>

Expression may be a numeric value or a math expression.

This condition is evaluated to true if <expression 1> is less or equal to <expression 2>

int a = 5;
int b = 6;
int c = 2 + 3;
Boolean b1 = (a <= b);
Boolean b2 = (a <= c);
System.out.println(b1);
System.out.println(b2);

b1 is equal to true because a <= b (5<=6).

b2 is equal to true because a is less or equal c (a= c = 5)

The “>” JAVA operator: this JAVA operator is the “greater than” operator. It is used to test if an expression is greater than another expression.

The condition is written in this way: <expression1> > <expression2>

Expression may be a numeric value or a math expression.

This condition is evaluated to true if <expression 1> is greater than <expression 2>

int a = 7;
int b = 6;
int c = 2 + 5;
Boolean b1 = (a > b);
Boolean b2 = (a > c);
System.out.println(b1);
System.out.println(b2);

b1 is equal to true because a > b (7 > 6).

b2 is equal to false because a isn’t greater than c (a= c = 7)

 

The “>=” JAVA operator: this JAVA operator is the “greater or equal” operator. It is used to test if an expression is greater or equal to another expression.

The condition is written in this way: <expression1> >= <expression2>

Expression may be a numeric value or a math expression.

This condition is evaluated to true if <expression 1> is greater or equal to <expression 2>

int a = 7;
int b = 6;
int c = 2 + 5;
Boolean b1 = (a > b);
Boolean b2 = (a > c);
System.out.println(b1);
System.out.println(b2);

b1 is equal to true because a >= b (7 >= 6).

b2 is equal to true because a is greater or equal to c (a= c = 7)

The Logical JAVA Operators: 

The “&&” JAVA operator: this JAVA operator is the “logical and” operator.

The condition is written in this way: <expression1> && <expression2>

This condition is evaluated to true if both <expression 1> and <expression 2> are evaluated to true.

int a = 7;
int b = 6;
int c = 2 + 5;
Boolean b1 = (a > b) && (c< a);
Boolean b2 = (a > b) && (c > b);
System.out.println(b1);
System.out.println(b2);

b1 is equal to false because  (c < a) is evaluated to false although (a > b) is true.

b2 is equal to true because a is greater than b (7 > 6) and c is greater than b (7 > 6)

The “||” JAVA operator: this JAVA operator is the “logical or” operator.

The condition is written in this way: <expression1> || <expression2>

This condition is evaluated to true if at least one of <expression 1> or <expression 2> is evaluated to true.

int a = 7;
int b = 6;
int c = 2 + 5;
Boolean b1 = (a > b) || (c< a);
Boolean b2 = (b > a) || (b > c);
System.out.println(b1);
System.out.println(b2);

b1 is equal to true because  (a > b) is true.

b2 is equal to false because the two conditions are false.

 

The “!” JAVA operator: this JAVA operator is the “logical not” operator.

The condition is written in this way:! <expression>

This
condition is evaluated to true if <expression> is evaluated to false. (the
! <expression> is equal to true)
Boolean a = true;
Boolean b = false;
Boolean b1 = ! a;
Boolean b2 = ! b;
System.out.println(b1);
System.out.println(b2);

b1 is equal to false and b2 is equal to true.

The not operator inverses the value of a Boolean value.

Post a Comment

Previous Post Next Post