operators in java

Operators in Java | Java Tutorial

In Java, an operator is a set of symbols that perform operations on one, two, or three operands and then returns a result. The operators are categorized and listed according to precedence order. The most common use of Java operators is to manipulate primitive data types.

  • Operators are used to perform operations on variables and values.
  • In Java, an operator is a symbol that is used to perform operations. For example, +, -, *, /, and so on.

bitwise operator in java, bitwise operator in java, bitwise operator in java, bitwise operator in java, bitwise operator in java, bitwise operator in java, bitwise operator in java

Java provides a variety of operators that can be used as needed. They are classified according to the functionality they offer. Some of the types are as follows:

  1. Unary Operator,
  2. Arithmetic Operator,
  3. Shift Operator,
  4. Relational Operator,
  5. Bitwise Operator,
  6. Logical Operator,
  7. Ternary Operator,
  8. Assignment Operator.

Unary Operator

Java unary operators are those that require only one operand to perform any operation such as increment, decrement, negation, and so on. It is made up of various arithmetic, logical, and other operators that all operate on the same operand.

  • The Java unary operators require only one operand.
  • – :A unary minus that is used to negate values.
  • + :A unary plus denotes a positive value. When the operand is byte, char, or short, it performs an automatic conversion to int. This is referred to as unary numeric promotion.

++ :Increment operator, used to increase a value by one. The increment operator includes two types.

  • Post-Increment: The value is first used to compute the result before being incremented.
  • Pre-Increment: The value is first incremented, and then the result is computed.

— : The Decrement operator is used to decrement the value by one. The decrement operator includes two types.

  • Post-decrement: The value is first used to compute the result before being decremented.
  • Pre-Decrement: The value is decremented first, and then the result is computed. 

Arithmatic Operator

Java arithmetic operators are used to perform operations such as addition, subtraction, multiplication, and division. They serve as fundamental mathematical operations.
They are used to perform basic arithmetic on primitive data types.

  • * Multiplication,
  • / Division ,
  • % Modulo,
  • + Addition ,
  • – Subtraction.

Shift Operators

These operators are used to shift the bits of a number to the left or right, multiplying or dividing the number by two. When we need to multiply or divide a number by two, we can use them. 

  • << , Left shift operator: shifts the number’s bits to the left and fills any voids with 0as a result. Similar to multiplying the number by a power of two.
  • >> , Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left. The leftmost bit is determined by the sign of the initial number. Similar to dividing by a power of two.
  • >>> , Unsigned Right shift operator: shifts the number’s bits to the right and fills any voids left by doing so. The bit on the left is set to 0.

Relational Operator

These operators are used to test for equality, greater than, and less than. They return a boolean result after a comparison and are commonly used in looping statements and conditional if else statements. 

Some relational operators are:

  • ==, Equal to: returns true if the left and right hand sides are equal.
  • !=, Not Equal to : returns true if the left and right hand sides are not equal.
  • < , less than : returns true if left hand side is less than right hand side.
  • <=, less than or equal to :returns true if left hand side is less than or equal to right hand side.
  • >, Greater than: returns true if the left side is greater than the right side.
  • >=, Greater than or equal to: returns true if the left side is greater than or equal to the right side.

Bitwise Operator

Bitwise operators are used to manipulate individual bits in a number. They are compatible with all integral types (char, short, int, etc). They are used in binary indexed tree for update and query operations.

&, Bitwise AND operator: returns bit by bit AND of input values. |, Bitwise OR operator: returns bit by bit OR of input values.
, Bitwise Complement Operator: A unary operator that returns the one’s complement representation of the input value, i.e. with all bits inversed.

&, Bitwise AND operator returns bit by bit AND of input values.
|, Bitwise OR operator returns bit by bit OR of input values.
~, Bitwise Complement Operator A unary operator that returns the one’s complement representation of the input value, i.e. with all bits inversed.

Logical Operator

Logical operators are used to perform “logical AND” and “logical OR” operations, which are analogous to the AND and OR gates in digital electronics. One thing to keep in mind is that if the first condition is false, the second condition is not evaluated, resulting in a short-circuiting effect. Used extensively to test for a variety of conditions in order to make a decision.

  • When both conditions are met, &&, Logical AND : returns true.
  • ||, Logical OR : returns true if at least one of the conditions is met.

Ternary Operator

The ternary operator in Java allows you to write an if statement on a single line of code. A ternary operator can return either true or false. It returns a specific value based on whether the statement is true or false.

if (value >= 100) {
	String result = "This value is over 100."
} else {
	String result = "This user is under 100."
}

Assignment Operator

In Java, assignment operators are used to assign values to variables. For example, int age; age = 5; The assignment operator = is used here. It assigns the value to the variable on its right to the variable on its left.

  • += is used to combine the left and right operands and then assign the result to a variable on the left.
  • -=, for subtracting the left operand from the right operand and then assigning the result to the left operand’s variable.
  • *=, for multiplying the left operand by the right operand and then assigning the result to the left operand’s variable.
  • /= is used to divide the left operand by the right operand and then assign the result to a variable on the left.
  • percent =, for multiplying the modulo of the left operand by the modulo of the right operand and then assigning it to a variable on the left.

logical operators in java, logical operators in java, logical operators in java, logical operators in java, logical operators in java, logical operators in java, logical operators in java, logical operators in java, logical operators in java

You might also like:

Java Data Types | Primitive and Non-Primitive Data Types

Difference between JDK, JRE, JVM | Java Tutorial

We hope that this article will assist you in understanding all about the Operator in java. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

This Post Has 2 Comments

Leave a Reply