×

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are very important for clearing the coding test in interview point of view.

Binary Operators

An operator known as a binary operator that manipulates two operands to get a result. Operators offer a simple way to evaluate integer data or character strings and are specified by special elements or by keywords.

The binary operators will be shown as follows:

A operator B

Where A = Operand1 operator operator2

B = Operand2 operator operator2

Some typical binary operations used in computing include

  • Equal (==)
  • Not equal (!=)
  • Less than (<)
  • Greater than (>)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  • Logical AND (&&)
  • Logical OR (||)
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

Let us discuss each one of them.

Equal (==)

In Java, the == operator is a particular class of relational operator that is used to examine equality relations. After the comparison, it gives a Boolean result, which is frequently used in looped statements and logical if-else expressions.

Let us understand it with an example program.

File name: Equal.java

// Programming in Java for the == operation
import java.io.*;
public class Equal {
public static void main(String[] args)
{
// declaring fundamental values
int k = 8;
int l = 8;
int m = 9;


// Using the == operator to compare two things
System.out.println("Are " + k + " and " + bl
+ " equal? " + (k == l));


// Using the == operator to compare k and m
System.out.println("Are " + l + " and " + m
+ " equal? " + (l == m));
}
}

Output

Are 8 and 8 equal?  true
Are 8 and 9 equal?  false

Not Equal (!=)

Java's Not Equal Relational Operators would be used to determine whether the left operand and the second operand are not equal.

The Not Equal operation is represented by the symbols !=.

As seen in the following, the Not Equal operator requires two operands: the left operand and the right operand.

A != B

Let us understand it with an example program

File name: Notequal.java

public class Notequal {
    public static void main(String[] args) {
        int f = 10;
        int c = 17;
        if (f != c) {
            System.out.println("f and c are not equal.");
        } else {
            System.out.println("f and c are equal.");
        }
    }
}

Output

f and c are not equal

Less Than (<)

The very first operand is compared to the second operand to see if it is less or not. When the operand on the left is less than the operand on the right, the operator returns true. In contrast to the greater-than operator, it operates differently.

Let us understand it with an example.

File name: Lessthan.java

// Code in Java to Demonstrate the Less Than Operator
import java.io.*;
class Lessthan {
                   public static void main(String[] args)
{
Int d = 45, e = 12
                           // Two variables are compared, and the resulting boolean value is reported.
System.out.println("d < e: " + (d < e))
}
}

Output

d < e: False

Greater Than (>)

This checks whether the first operand is greater than the second operand or not. The operator returns true when the operand at the left-hand side is greater than the right-hand side. 

Let us understand it with an example.

File name: Greaterthan.java

// Code in Java to Demonstrate the Greater Than Operator
import java.io.*;
class Greaterthan {


public static void main(String[] args)
{
int r= 22, d= 20;
// displaying the corresponding boolean value and comparing variables r and d
System.out.println("r > d: " + (r > d));
}
}

Output

r > d: true

Greater Than or Equal to (<=)

This determines if the first operand exceeds or is identical to the second operand. When the operand on the left is equal or greater to the operand on the right, the operator returns true.

Let us understand it with an example.

File name: Greatequal.java

// Greater than or equals to Operator in Java Program
import java.io.*;
class Greatequal {
public static void main(String[] args)
{
int r=19, m=13;
// showing the corresponding boolean value and comparing variables var1 and var2
System.out.println("r >= m: "+ (r >= m));
}
}

Output

r>=m: true

Less than or Equal to(<=)

The first operand must be smaller than or equal to a second operand for this to be true. When the operand on the left is less than and equal to the operand on the right, the operator returns true.

Let us understand it with an example.

File name: Lessequal.java

// Java Example of Less Than or Equal To Operation
import java.io.*;
class GFG {
public static void main(String[] args)
{
int k = 10, p = 11;


// displaying the corresponding boolean value and comparing p and k
System.out.println("p <= k: "
+ (p <= k));



}
}

Output

p <= k: false

Logical AND

When both of the requirements being considered are met or are true, this operator returns true. The operator produces a false result if any one of those two yields false. Cond1 && cond2 yields true if both condition 1 and condition 2 are true, to put it simply (i.e. non-zero).

Let us understand it with an example.

File name: LogicalAND

// Example Java code for the logical AND operator
import java.io.*;
class LogicalAND {
public static void main(String[] args)
{
// defining variables
int a = 10, b = 20, c = 20, d = 0;
System.out.println("V1 = " + a);
System.out.println("V2 = " + b);
System.out.println("V3 = " + c);


// using the AND logic to confirm two criteria
if ((a < b) && (b == c)) {
d = a + b + c;
System.out.println("The sum is: " + d);
}
else
System.out.println("False conditions");
}
}

Output

V1 = 10
V2 = 20
V3 = 20
The sum is: 50

Logical OR

When one of the two requirements being considered is met or is true, this operator returns true. The operator produces a true result indeed if a single of the two provides true. Both constraints must return false for the result to be false.

Let us understand it with an example.

File name: LogicalOR.java

// An example of the logical OR operator in Java
import java.io.*;
class Logical {
public static void main(String[] args)
{
// defining variables
int a = 10, b = 1, c = 10, d = 30;
System.out.println("V1 = " + a);
System.out.println("V2 = " + b);
System.out.println("V3 = " + c);
System.out.println("V4 = " + d);
// comparing two restrictions with logical OR
if (a > b || c == d)
System.out.println("One or both + the conditions are true");
else
System.out.println("Both the + conditions are false");
}
}

Output

V1 = 10
V2 = 1
V3 = 10
V4 = 30
One or both the conditions are true

Addition (+)

 This binary operator is employed to combine two operands.

Let us understand it with the help of a program.

File name: Addition

// Example Java code for the addition operator
import java.io.*;
class Addition {
public static void main(String[] args)
{
// defining  variables
int v1 = 10, v2 = 20, sum ;
                          // Displaying them
System.out.println("v1 = " + v1);
System.out.println("v2 = " + v2);


sum = num1 + num2;
System.out.println("The sum = " + sum);
}
}

Output

v1 = 10
v2 = 20
The sum = 30

Subtraction

Two operands can be subtracted using this binary operator.

Let us understand it with an example.

File name: Subtraction.java

// An example of the Subtraction operator in Java
import java.io.*;
class Subtraction {
public static void main(String[] args)
{
// defining variables
int v1 = 20, v2 = 10, sub;



System.out.println("v1 = " + v1);
System.out.println("v2 = " + v2);
sub = v1 - v2;
System.out.println("Subtraction = " + sub);
}
}

Output

v1 = 20
v2 = 10
Subtraction = 10

Multiplication

Two operands can be multiplied using this binary operator.

Let us understand it with an example.

File name: Multiply.java

// Example Java code for the Multiplication operator
import java.io.*;
class Multiply {
public static void main(String[] args)
{
// defining variables
int v1 = 20, v2 = 10, mult = 0;
System.out.println("v1 = " + v1);
System.out.println("v2 = " + v2);
mult = v1 * v2;
System.out.println("Multiplication = " + mult);
}
}

Output

v1 = 20
v2 = 10
Multiplication = 200

Division (/)

Dividing the first operand (dividend) by the subsequent operand (divisor) using this binary operator to get the quotient as the outcome.

Let us understand it with an example.

File name: Divide.java

// An example of division operator in Java
import java.io.*;
class Divide {
public static void main(String[] args)
{
// defining variables
int v1 = 20, v2 = 10, div = 0;
System.out.println("v1 = " + v1);
System.out.println("v2 = " + v2);
div = v1 / v2;
System.out.println("Division = " + div);
}
}

Output

v1 = 20
v2 = 10
Division = 2

Related Topics

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.