Arithmetic Operators, Relational Operators, Logical Operators

Arithmetic Operators, Relational Operators, Logical Operators

Program operators in the Java programming language are reserved program symbols or special characters used by a Java developer to perform custom expressions, conditions, or operations on program variables and values. Operators used in Java programming are divided into several categories based on their function. Here, we will better understand the most popular arithmetic, relational, and logical operators in Java programming.

Arithmetic Operators, Relational Operators, Logical Operators

So, let’s take a closer look at arithmetic, relational, and logical operators in Java programming.

  • Arithmetic Java operators.
  • Relational Java operators.
  • Logical Java operators.

Arithmetic Java operators.

Arithmetic operators in Java programming are used to apply basic mathematical operations such as add, subtract, multiply, divide, and modulus. For example, arithmetic operators in Java are used with numeric data type variables such as int, float, double, and long.

Java Arithmetic Operator Details.

OperatorArithmetic Operators DescriptionExample
+Add arithmetic operator used to Add one or more integer value  p + q
Subtraction arithmetic operator used to subtract larger value with smaller valuep – q
*Multiplication arithmetic operator used to multiply two different integer valuep * q
/Division arithmetic operator used to divide large value with smaller numeric value integer or floating-pointp / q
%Modulus arithmetic operator used to get remainder of any value (it displays after division lager value with remainder)p % q

public class Main

{

public static void main(String[] args) {

int p = 25, q = 7;

System.out.println(“The addition of p and q is – ” + (p + q)); // Result is – 25 + 7 = 32

System.out.println(“The subtraction of p and q is – ” + (p – q)); // Result is – 25 – 7 = 18

System.out.println(“The multiplication of p and q is – ” + (p * q)); // Result is – 25 * 7 = 175

System.out.println(“The division of p and q is – ” + (p / q)); // Result is – 25 / 7 = 3 (integer division)

System.out.println(“The modulus of p & q is – ” + (p % q)); // Result is – 25 % 7 = 4 (remainder)

}

}

Element of Arithmetic Operator.

Division Arithmetic – Here, when both operands are integer values, the integer values ​​are divided, which removes the reminder value during division. If any of the operands here is a floating-point type such as float, double, or in a data type format, the arithmetic operator result will be displayed as a floating-point number.

Relational Java Operators.

Relational operators in the Java programming language are used to compare two integer numeric values, logic, or custom condition expressions defined by the programmer. Relational operators in Java return a Boolean value, which can be true or false, depending on a user-defined program condition. Relational operators help Java programmers make specific decisions or control the default behavior of a program.

Relational Java Operators Details.

OperatorRelational Operators DescriptionExample
==Equal to Relational Operators used to equal one value to another valuep == q
!=Not equal to Relational Operators used to find out not equal program valuep != q
Greater than Relational Operators used to find out greater numeric value between two valuesp > q
Less than Relational Operators used to find out less than numeric value between two valuesp < q
>=Greater than or equal to Relational Operators used to check values is greater than or equal to value in programp >= q
<=Less than or equal to Relational Operators used to check given value is less than or equal to valuep <= q

Example of Relational Java Operators.

public class Main

{

public static void main(String[] args) {

int p = 7, q = 4;

System.out.println(“The equal value p == q – ” + (p == q)); // Result is false

System.out.println(“The not equal value p != q – ” + (p != q)); // Result is true

System.out.println(“The greater than value p > q – ” + (p > q)); // Result is true

System.out.println(“The less than value p < q – ” + (p < q)); // Result is false

System.out.println(“The greater than or equal to the value p >= q – ” + (p >= q)); // Result is true

System.out.println(“The less than or equal to the value p <= q – ” + (p <= q)); // Result is false

}

}

Element of Relational Java Operators.

Relational operators in Java programs are commonly used in if-else block statements, control flow, for, while, or do-while loops, or to compare or make decisions on any type of logical value.

Logical Java Operators.

Logical operators in the Java programming language are used to perform logical conditions or operations on Boolean values. Logical operators in Java programs match or reject user-defined logic or conditions, and are useful for controlling the default flow of a program based on multiple conditions in a given program.

Logical Java Operators Details.

OperatorLogical Operators DescriptionExample
&&Logical AND operator used to Returns true if both conditions are true in given programp && q
||Logical or operator used to display result true only one condition true in given expressionp || q
!Logical NOT operator used to Returns the Reverses the boolean value inverts!p

Example of Logical Java Operators.

public class Main

{

public static void main(String[] args) {

boolean p = true, q = false;

System.out.println(“The logical AND operation is p && q – ” + (p && q)); // Result is false (both conditions must be true for AND)

System.out.println(“The logical OR operation is p || q – ” + (p || q)); // Result is true (only one condition must be true for OR)

System.out.println(“The logical NOT operation is !p – ” + (!p)); // Result is – false (condition NOT reverses the boolean value)

}

}

Element of Logical Java Operators.

  • Logical AND (&&) operator – The logical AND operator in a Java program returns a true result only if both operands are true at the same time.
  • Logical OR (||) operator – It returns a true result if any one of the operands is true in a Java program.
  • Logical NOT (!) operator – It reverses a boolean value in a Java program, returning it false if the condition is true, and reversing it if the condition is not true.

Combining Multiple Java Operators in an Expression.

More complex conditions and calculations can be developed by adding multiple logical and other operators to a condition expression in a Java program. For example, Java programmers can use arithmetic, relational, and logical operators together to test or analyze multiple operator conditions in a single program.

Example of combining arithmetic, relational, and logical operators in a Java program.

public class Main

{

public static void main(String[] args) {

int p = 8, q = 16;

boolean output;

/ here it combines logical and relational operators into a single program

output = (p < q) && (q > 12); // here it displays true (when both conditions must be true at the same time)

System.out.println(“Output of the combined logical and relational operator is – ” + output);

// Combined arithmetic and relational operators

boolean isGreater = (p * 4) > (q / 4); // here is (16 > 8) -> true

System.out.println(“Here Is p * 4 > q / 4 ? ” + isGreater);

}

}

Here, the logical operator && is used to group two relational conditions, and the arithmetic operator is used to perform the calculations required for comparison.

Detail Explanation abut Arithmetic Operators, Relational Operators, Logical Operators.

Operator TypeOperator symbolOperator Description
Arithmetic operator Add+Used to Addition two value
SubtractUsed to Subtraction two value
Multiply*Used to Multiplication two value
Division/Used to Division (integer or floating-point) two value
Modulus%Used to Modulus (remainder after division) two value
Relational operator Equal to==Used to find out Equal to two values
Not Equal!=Used to find out Not equal to value
Geater thanUsed to find out Greater than value
Less thanUsed to find out Less than value
Greater than or equal to>=Used to find out Greater than or equal to value
Less than or equal to<=Used to find out Less than or equal to value
Logical operator AND&&Logical AND must check both (both conditions must be true) at a same time
Logical OR||Logical OR must check one (one conditions must be true) at a time
Logical NOT!Logical NOT it reverse or (inverts the boolean value)

Arithmetic Operators, Relational Operators, Logical Operators Conclusion.

  • Arithmetic operators are used in Java programs to perform basic mathematical calculations.
  • Relational operators are used in Java programs to compare given variable values ​​and define relationships between them.
  • Logical operators are used in Java programs to group multiple Boolean condition expressions, which help in making a complex decision based on the condition given by the programmer.

Leave a Reply