JAVA – Logical /Boolean Operator


The relational operators work well in conditions where only a single condition needs to be checked. Logical operators either return a True value or a False value based on the state of the Variables. The six logical or Boolean, operators used in the Java programming language are:

  • AND
  • conditional AND
  • OR
  • conditional OR
  • exclusive OR
  • NOT

A logical operator accepts only arguments declared as Boolean data types. Also, these operators return value of Boolean data type.

These operators unlike the relational operators, that you already know, are used to check whether a set of conditions is true or not. In such as case, if statements are used. The code snippet below shows example of this:

if (num_var_1 == 4) {
  if (num_var_2 != 4) {
     System.out.println("Both conditions are true.");
   }
}

However, as you can see this is not a user-friendly code, and is both harder to write and read. Such a code snippet becomes difficult to manage when multiple conditions are used. Let’s now look at the use of the following three conditional operators:&&, || and !.

The logical operator && combines two Boolean values and gives a True result if and only if both of its operands are true. This is shown in the following code snippet:

Boolean c;
num_var_1 = 51 > 49 && 13 < 13; // num_var_1 is true
num_var_1 = 44 > 52 && 7 < 9; // num_var_1 is now false

Similarly, the logical operator || combines two Boolean variables or expressions. It returns a true result only if either or both of its operands are true. Let’s look at the following code snippet which shows the rule:

Boolean d;
num_var_4 = 51 > 49 || 73 < 91; // num_var_4 is true
num_var_4 = 49 > 51 || 73 < 91; // num_var_4 is still true
num_var_4 = 49 > 51 || 73 > 91; // now num_var_4 is false

The third and the last logic operator which we will look at is “!”  Which denotes “not”. The “!” logical operator reverses the value of a Boolean expression.

According to this logical operator:

if x is true !x is false.

If x is false  !x is true.

boolean bol_var_1;
bol_var_1 = !(4 > 5); // bol_var_1 is false
bol_var_1 = !(5 > 4); // bol_var_1 is true

As already stated logical operators allow you to test and use multiple conditions. For example, we can use the logical operators in the code snippet we saw above as follows:

if (var_num_1 == 2 && var_num_2 != 2) {
  System.out.println("Both conditions are true.");
}

Let’s look at a code snippet Demo_of_Logical_Operators which makes use of multiple logical operators:

public class Demo_of_Logical_Operators

{

public Demo_of_Logical_Operators () {

boolean bol_var_1 = true;

boolean bol_var_2 = false;

System.out.println(“bol_var_1 & bol_var_2 : ” + (bol_var_1 & bol_var_2));

System.out.println(“bol_var_1 && bol_var_2 : ” + (bol_var_1 && bol_var_2));

System.out.println(“bol_var_1 | bol_var_2 : ” + (bol_var_1 | bol_var_2));

System.out.println(“bol_var_1 || bol_var_2: ” + (bol_var_1 || bol_var_2));

System.out.println(“bol_var_1 ^ bol_var_2 : ” + (bol_var_1 ^ bol_var_2));

System.out.println(“!bol_var_1 : ” + (!bol_var_1));

}

public static void main(String args[]) {

new Demo_of_Logical_Operators ();

The table given below defines the different Boolean operators, if x and y are both Boolean expressions.

x y !x x & y

x && y

x | y

x || y

x ^ y
true true false true true false
true false false false true true
false true true false true true
false false true false false false

JAVA – Relational Operators


The relational operators are used to determine if one operand is equal to, greater than, less than, or not equal to the other operand with which it is compared. Just like mathematical operators, most of these operators are just like the normal operators we use in the everyday life. But certain operators are used in a different manner also.

For example, in place of “=” operator that we generally use for comparing two operands are equal, the relational operators in the Java programming language use  “==’.

The relational operators are defined as binary expressions, while their operands are numeric expressions.

When applying these operators, the rules that apply to the binary operators are applied and their evaluation returns a numeric value. Not only this, the operator precedence of relational operators is higher than assignment operators but lower than arithmetic operators.

The six relational operators used in Java and their uses are listed in the table below:

Relational Operator Description
Equal to ==
Not equal to !=
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=

Next, let’s look at the code for the program demo_of_relational_operator that shows the use of the comparison operators.

class demo_of_relational_operator {

public static void main(String[] args){

int num_var_1 = 32;

int num_var_2 = 47;

if(num_var_1 == num_var_2) System.out.println(“num_var_1 == num_var_2″);

if(num_var_1 != num_var_2) System.out.println(“num_var_1 != num_var_2″);

if(num_var_1 > num_var_2) System.out.println(“num_var_1 > num_var_2″);

if(num_var_1 < num_var_2) System.out.println(“num_var_1 < num_var_2″);

if(num_var_1 <= num_var_2) System.out.println(“num_var_1 <= num_var_2″);

}

}

Output:

num_var_1 != num_var_2

num_var_1 < num_var_2

num_var_1 <= num_var_2

Next, let’s look at another code snipped displaying the use of different relational operators.

public class Demo_of_Relational_Operators

{

public Demo_of_Relational_Operators ( )

{

int num_var_1 = 10, num_var_2 = 5;

System.out.println(”num_var_1 > num_var_2 : “+(num_var_1 > num_var_2 ));

System.out.println(”num_var_1 < num_var_2 : “+(num_var_1 < num_var_2 ));

System.out.println(”num_var_1 >= num_var_2 : “+(num_var_1 >= num_var_2 ));

System.out.println(”num_var_1 <= num_var_2 : “+(num_var_1 <= num_var_2 ));

System.out.println(”num_var_1 == num_var_2 : “+(num_var_1 == num_var_2 ));

System.out.println(”num_var_1 != num_var_2 : “+(num_var_1 != num_var_2 ));

}

public static void main(String args[]){

new Demo_of_Relational_Operators ();

}

}

JAVA – Arithmetic Operators


The Java programming language provides operators that perform simple mathematical operations such as addition, subtraction, multiplication, and division. These operators resemble the basic mathematics symbols that we all are aware of. Unlike the normal mathematical symbols, the “%”operator divides one operand by another and returns a remainder in the result.

The Java programming languages uses the following symbols:

  • + Additive operator: This operator is not only used for addition, but is also used for String concatenation purposes.
  • - Subtraction operator: This operator is used for subtracting the operands.
  • * Multiplication operator:  This operator is used for multiplying the operands.
  • / Division operator: This operator is used for dividing two or more operands.
  • % remainder operator: This operator is used for obtaining the remainder by dividing one operand with another.

Let’s now look at a small program, Demo_of_operator_program, which shows the use of different arithmetic operators.

class Demo_of_operator_program {

public static void main (String[] args){

int result_operator = 20 + 42; // The result_operator is now 62

System.out.println(result_operator);

result_operator = result_operator – 22; // The result_operator is now 40

System.out.println(result_operator);

result_operator = result_operator * 3; // The result_operator is now 120

System.out.println(result_operator);

result_operator = result_operator / 2; // The result_operator is now 60

System.out.println(result_operator);

result_operator = result_operator + 10; // The result_operator is now 70

result_operator = result % 14; // The result_operator is now 5

System.out.println(result_operator);

}

}

The arithmetic operators listed above can be combined with the simple assignment operator to create compound assignments. For example the compound assignments such as, y+=1; and y=y+1; both increment the value of y by 1.

Not only this, you can also use the + or the additive operator for joining or concatenating two strings together. The Demo_of_Concat_Stings program displayed below displays a sample of string concatenation:

class Demo_of_Concat_Stings {

public static void main(String[] args){

String first_normal_String = “This is an”;

String second_normal_String = ” arithmetic operator”;

String third_concataneted_String = first_normal_String+second_normal_String;

System.out.println(third_concataneted_String);

}

}

This program displays the result: “This is an arithmetic operator.” which gets stored in the variable third_concataneted_String.

Operators in Java


Computer operators are special symbols that perform specific operations on operands to return a result. Most computer programming languages support pre-defined set of operators, similar in function to mathematical operators.

Many a times, these operators are built-into a programming language, while at other times these may be defined in the program itself.  Therefore, languages such as C support built-in operators, while C++ and Java contain program-defined operators.

Operator precedence and associativity are defined in computer programming language specifications. Similarly, the position of the computer operator with respect to the computer operands can be prefix, postfix, or infix.

In the Java programming language, the operators act according to their precedence. Therefore, it is important that we know in advance about the operators with highest precedence, as these operators are evaluated before the operators with lower precedence. On the other hand, all the operators appearing on the same line have equal precedence. The Java compiler evaluates all operators, except the binary operators from left to right. Only the assignment operators are evaluated from right to left.

The following table lists the operators according to their precedence.

Continue reading Operators in Java