Java Operators Tutorial – Operators In Java Tutorial For Beginners

Java Operators Tutorial – Operators In Java Tutorial For Beginners from Coding compiler. Here you will learn about arithmetic, boolean, relational, ternary and bitwise operators. Let’s start learning Java now.!

Java Operators Tutorial For Beginners

Java operators can be classified into arithmetic operators, boolean operators, relational operators, ternary operators, bitwise operators. Here we will discuss on each individual java operator.

Types of Java Operators

  1. Java arithmetic operators
  2. Java Boolean operators
  3. Java relational operators
  4. Java ternary operator
  5. Java bitwise operator

 

Java Tutorial – Java Arithmetic Operator

 

Use arithmetic operators in mathematical expressions.

All arithmetic operators

The following table lists the arithmetic operators:

Operator result
+ addition
Subtraction
* multiplication
/ division
% remainder
++ Increased
+= Addition allocation
-= Subtraction allocation
*= Multiplicative allocation
/= Allocation allocation
%= Modulus distribution
Self-reduction

The operands of arithmetic operators must be numeric. You cannot boolean use arithmetic operators on char types , but you can use them on types.

Basic arithmetic operations are addition, subtraction, multiplication, and division. Their behaviour is as you would expect. The minus operator also has a unary form that cancels its single operand.

The quick demo below shows how to do simple calculations of basic arithmetic operators in Java.


Public class Main {

 Public static void main(String args[]) {

   System.out.println( “Integer Arithmetic” );
    int a = 1 + 1;
    int b = a * 3;
    int c = b / 4;
    int d = c – a;
    int e = -d;
   System.out.println( “a = ” + a);
   System.out.println( “b = ” + b);
   System.out.println( “c = ” + c);
   System.out.println( “d = ” + d);
   System.out.println( “e = ” + e);
   
   Int x = 42;
   System.out.println( “x mod 10 = ” + x % 10);
    double y = 42.25;

   System.out.println( “y mod 10 = ” + y % 10);
 }
}

When you run this program, you will see the following output:

The modulo operator returns the remainder of the division operation. Modulo operators can be applied to floating point types as well as integer types.

 

Java compound allocation operator

The statement is as follows

a = a + 4;

Can be rewritten as

a += 4;

Both statements perform the same operation: they increase a by the value of 4.

Any form of the statement

Var = var op expression;

Can be rewritten as

Var op= expression;

Here is a sample program that shows several op=operator assignments:


Public class Main {

 Public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = 3;

   a += 1;
   b *= 2;
   c += a * b;
   c %= 3;
   System.out.println( “a = ” + a);
   System.out.println( “b = ” + b);
   System.out.println( “c = ” + c);

 }
}

 

Java increment and decrement operators

++And is the Java increment and decrement operator. The increment operator increments ++its operand by 1. The decrement operator decrements its operand by 1.

The difference between the increment and decrement operators:

For example, this statement:

x = x + 1;

This can be done by using the increment operator:

x++;

This sentence:

x = x – 1;

Equivalent

X–;

The increment and decrement operators are unique because they can appear as suffixes and prefixes. In the suffix form, they follow the operands, for example, i++. In the prefix form, they precede the operand, for example – i.

When the increment and/or decrement operator is a part, a larger expression appears between the two forms. In the prefix form, the operand is incremented or decremented before the value is used in the expression. In the suffix form, use the value in the expression and then modify the operand.

The following table summarizes the differences between before and after increment and decrement operations:

Initial value of x expression The final value of y The final value of x
5 y = x ++ 5 6
5 y = ++ x 6 6
5 y = x– 5 4
5 y = -x 4 4

E.g:

x = 42;
y = ++x;

Set to 43, because the increment occurs before x is assigned to y. Therefore, the line

y = ++x;

Is equivalent to these two statements:

x = x + 1;
y = x;

However, when writing this way,

x = 42;
y = x++;

Get the value of x before executing the increment operator, so the y of the value is 42.

In both cases, x is set to 43. line

y = x++;

Is equivalent to these two statements:

y = x;
x = x + 1;

The following program demonstrates the incremental operator.


Public class Main {

 Public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = ++b;
    int d = a++;

   System.out.println( “a = ” + a);
   System.out.println( “b = ” + b);
   System.out.println( “c = ” + c);
   System.out.println( “d = ” + d);

 }
}

Java Tutorial – Java Boolean Operators

 

Boolean operators operate on boolean operands.

Logical operator list

The table below lists all Java Boolean logical operators.

Operator result
& Logical AND
| Logical or
^ Logical exclusive OR (exclusive OR)
|| Short circuit OR
&& Short circuit AND
! Logical unary NOT
&= AND distribution
|= OR allocation
^= XOR allocation
== equal
!= not equal to
? : Ternary if-then-else

 

True table

The following table shows the effect of each logical operation:

A B A | B A& B A ^ B !A
False False False False False True
True False True False True False
False True True False True True
True True True True False False

The following program demonstrates the Boolean logic operator.


Public class Main {
  public static void main(String args[]) {
    boolean a = true;
    boolean b = false;
    boolean c = a | b;
    boolean d = a & b;
    boolean e = a ^ b;
    boolean f = ( !a & b) | (a & !b);
    boolean g = !a;
   System.out.println( ” a = ” + a);
   System.out.println( ” b = ” + b);
   System.out.println( ” a|b = ” + c);
   System.out.println( ” a&b = ” + d);
   System.out.println( ” a^b = ” + e);
   System.out.println( “!a&b|a&!b = ” + f);
   System.out.println( ” !a = ” + g);

 }
}
]]>

Example

The following program demonstrates the bitwise logical operator:


Public class Main {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c = a | b;
    int d = a & b;
    int e = a ^ b;
    int f = ( ~a & b) | (a & ~b);
    int g = ~a & 0x0f;

   System.out.println( ” a = ” + a);
   System.out.println( ” b = ” + b);
   System.out.println( ” a|b = ” + c);
   System.out.println( ” a&b = ” + d);
   System.out.println( ” a^b = ” + e);
   System.out.println( “~a&b|a&~b = ” + f);
   System.out.println( ” ~a = ” + g);

 }
}

Here is the output of this program:

Java logical operator shortcut

When an operand true, the OR operator is true, regardless of the second operand Yes. When an operand false, the AND operator will lead to false, the operand Yes. If you use ||and &&, Java will not evaluate the right operand result, which can be determined separately by the left operand.

The following code shows how to use the short-circuit logical operator to ensure that the division is valid before evaluation:


Public class Main {
  public static void main(String[] argv) {
    int denom = 0;
    int num = 3;
    if (denom != 0 && num / denom > 10) {
     System.out.println( “Here” );
   } else {
     System.out.println( “There” );
   }
 }
}

Output:

If we want to change the fast behaviour of logical operators, we can use & and |.

Example 2

The following code uses a single & ensures that the incremental operation will be applied to whether it is equal to 1.


Public class Main {
  public static void main(String[] argv) {
    int c = 0;
    int e = 99;
    int d = 0;
    if (c == 1 & e++ < 100)
     d = 100;

   System.out.println( “e is ” + e);
   System.out.println( “d is ” + d);
 }
}

Java Tutorial – Java Relational Operators

 

The Java relational operator determines the relationship between two operands.

Relational operators list

The relational operators in Java are:

Operator result
== equal
!= not equal to
> more than the
< Less than
>= greater than or equal to
<= less than or equal to

For example, the following code snippet is completely valid. It compares two int values and assigns the result to a boolean c.


Public class Main {
  public static void main(String[] argv) {
    int a = 4;
    int b = 1;
    boolean c = a < b;

   System.out.println( “c is ” + c);
 }
}

The result of a <b (false) is stored in c.

Example

The result of the relational operator is a Boolean value. In the code below, System.out.println the result of the relational operator is output.

Public class Main {
  public static void main(String args[]) {
    // outcome of a relational operator is a boolean value
    System.out.println( “10 > 9 is ” + (10 > 9));
 }
}

Java Tutorial – Java ternary operator

 

?The operator is a ternary operator.

The Java ternary operator is basically a shorthand for a simple if statement.

Grammar

Has the following general form:

expression1 ? expression2 : expression3

expression1Can be booleanany expression that evaluates to a value. If expression1yes true, then it will be evaluated expression2. Otherwise, it will be evaluated expression3.

The calculated expression is the result of the operation. Requires expression2and expression3returns the same type, which can be invalid.

The following is an example of an operator:


public class Main {
 public static void main(String[] argv) {
   int denom = 10;
   int num = 4;
   double ratio;

   ratio = denom == 0 ? 0 : num / denom;
   System.out.println(“ratio = ” + ratio);
 }
}

Example

This is another program that demonstrates operators. It uses it to get the absolute value of the variable.


public class Main {
 public static void main(String args[]) {
   int i, k;
   i = 10;
   k = i < 0 ? -i : i;
   System.out.print(“Absolute value of “);
   System.out.println(i + ” is ” + k);

   i = -10;
   k = i < 0 ? -i : i;
   System.out.print(“Absolute value of “);
   System.out.println(i + ” is ” + k);

 }
}

Java Tutorial – Java Bitwise Operator

 

The bitwise operator acts on each bit of its operand. Java bitwise operators can be applied to integer types: long,int,short,char,byte.

Bitwise operators list

The following table lists all Java bitwise operators.

Operator result
~ Bitwise one dollar NOT
& 按位AND
| Bitwise or
^ Bitwise XOR
>> move to the right
>>> Shift right zero padding
<< move to the left
&= Bitwise AND assignment
|= Bitwise OR allocation
^= Bitwise different OR allocation
>> = Right shift assignment
>>> = Right shift zero fill allocation
<<= Shift to the left

The bitwise operator assignment combines the assignment with a bitwise operation. The following two statements are equivalent:

a = a >> 4;
a >>= 4;  

The following two statements are equivalent:

a = a | b;
a |= b;

The following program demonstrates the allocation of bitwise operators:


public class Main {
 public static void main(String args[]) {
   int a = 1;
   int b = 2;
   int c = 3;
   a |= 2;
   b >> = 2;
   c <<= 2;
   a ^= c;
   System.out.println(“a = ” + a);
   System.out.println(“b = ” + b);
   System.out.println(“c = ” + c);

 }
}

Java left shift operator

The left shift operator, << shifts one of all the bits to the time of the specified number to the left.

It has this general form:

value << num

The following code shifts the byte type variable.


public class Main {
 public static void main(String args[]) {
   byte a = 64, b;
   int i;
   i = a << 2;
   b = (byte) (a << 2);
   System.out.println(“Original value of a: ” + a);
   System.out.println(“i and b: ” + i + ” ” + b);
 }
}

Example

Each left shift has the effect of doubling the original value. The following procedure illustrates this:


public class Main {
 public static void main(String args[]) {
   int num = 0xFFFFFFF;

   for (int i = 0; i < 4; i++) {
     num = num << 1;
     System.out.println(num);

   }
 }
}

Java right shift operator

The right shift operator shifts >>all bits in the value to the right by a specified number of times.

Its general form is as follows:

value >> num

Num Specifies the number of positions to shift to the right.

The following code snippet shifts the value 32 to the right by two places:

public class Main {
 public static void main(String[] argv) {

   int a = 32;
   a = a >> 2;
   System.out.println(“a is ” + a);

 }
}

Java unsigned right shift

Java unsigned, right shift operator, >>>, always shifts zero to high order bits.

Its general form is as follows:

value >>> num

Num Specifies the number of positions to shift to the right.

The following code shows how to use unsigned right shift.

public class Main {
 public static void main(String[] argv) {
   int a = -1;
   a = a >>> 24;

   System.out.println(“a is ” + a);
 }
}

Related Java Tutorials & Interview Questions

Related Java Tutorials & Interview Questions
Introduction to Java Programming 21 Aricent Java Interview Questions
Java Keywords Tutorial For Beginners 53 Accenture Java Interview Questions
Java Data Types Tutorial For Beginners 399 Core Java Interview Questions
Java Performance Tuning Tips 581 Advanced Java Interview Questions
Core Java Multiple Choice Questions 60 Java Multiple Choice Questions And Answers

Leave a Comment