Java Operators Explained

Java Operators Explained

16 mins read1.3K Views Comment
clickHere
Updated on Mar 17, 2023 15:00 IST

In this article, we’ll cover several operators in Java with examples. Moreover, the article goes through operator precedence.

2022_05_java-operators.jpg

Java Operators define the operations or tasks provided by a given operator. Special symbols are called operators, and they perform operations on one, two, or three operands, and then return results. Knowing which operators have the highest precedence may be helpful for you as we explore the Java programming language’s operators.

Explore popular Java Courses

Contents

Explore free Java courses

Let’s begin with understanding the Java Operators.

Also, Read: Features of Java Programming Language

What are Java Operators?

The Java Operators define the operation or task that needs to perform. These operations can vary from mathematical to logical operations. ‘Operands’ are the entities on which the operators perform operations.

An n-ary operator works upon n operands. The operators are broadly classified into 3 categories:

  1. Unary Operators: It operates on a single (one) operand only. Ex: ++, –, etc.
  2. Binary Operators: These work upon two operands. Ex: +, -, <<, >>, etc.
  3. Ternary Operators: It uses 3 operands to perform its operations. Ex: ? operator who works like an if-else statement.

Also read: Variables in Java

Types of Operators in Java

There are broadly 5 types of Operators in Java:

types

Let’s understand them one by one.

Assignment Operator (=)

The assignment operator (=)  is a binary operator. It basically assigns the value of the operand on the right hand of an expression to the operand on the left side. 

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class Assignment_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n double x \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 4.67 \n <span style="color: #339933">\n ; \n <span style="color: #666666;font-style: italic">\n //assignment operator \n
\n <span style="color: #000066;font-weight: bold">\n double y \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Value of x: " \n <span style="color: #339933">\n +x \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
y \n <span style="color: #339933">\n =x \n <span style="color: #339933">\n ; \n <span style="color: #666666;font-style: italic">\n //assignment operator used to assign the value of x to y \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Value of y: " \n <span style="color: #339933">\n +y \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #666666;font-style: italic"> \n </span style="color: #339933"> \n </span style="color: #339933"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #666666;font-style: italic"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

ouput1

Arithmetic Operators

Arithmetic operators can be unary as well binary mathematical operators. They are further divided into 3 categories:

  • Basic Arithmetic Operators
  • Increment and Decrement Operators 
  • Shorthand Operators

Basic Arithmetic Operators

There are 5 basic Arithmetic Operators in Java. The elementary arithmetic operators include addition, subtraction, multiplication, division, and modulus.

OperatorSign Operator Name Operator Type Operation Performed Example
+ Addition Binary The addition of two operands from either side will give the same result. a+b
Subtraction Binary Subtracts the operand at the right side of the operator from the left side operand. The positional exchange of operands will give a different result. a-b
* Multiplication Binary Multiplication of two operands from either side will give the same result. a*b
/ Division Binary Dividing the operand at the left side of the operator by the operand at the right side. a/b
% Modulus Binary Returns the remainder on dividing the two operands. a%b
+ Unary Plus Unary Indicates the number is positive. It’s also implicit by default. +a
Unary Minus Unary Indicates the number is negative. -a

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class Arithmetic_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n double x \n <span style="color: #339933">\n = \n <span style="color: #339933">\n - \n <span style="color: #cc66cc">\n 4.67 \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n double y \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 2 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Addition: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n +y \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Subtraction: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n -y \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Multiplication: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n *y \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Division: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n /y \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Remainder: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (y \n <span style="color: #339933">\n %2 \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output2

Increment and Decrement Operators

The increment and decrement operators are unary operators (works on one operator only). The increment increases the value by 1. Whereas, the decrement decreases the value by 1.

OperatorSign Operator Name Operator Type Operation Performed Example
++ Increment Unary Increments the value of the operand by 1. ++a, a++
—  Decrement Unary Decrements the value of the operand by 1. –a, a–

These are further divided into 4 types:

  • Pre-increment: It first increments the value and then uses that value in the expression.
  • Post-increment: It first decrements the value and then uses that value in the expression.
  • Pre-decrement: It first uses the value of the operand in the expression and then decrements it.
  • Post-decrement: It first uses the value of the operand in the expression and then increments it.

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class IncDec_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n int x \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 2 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Value of x: " \n <span style="color: #339933">\n +x \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Post-increment Value of x++: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n ++ \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Pre-increment Value of ++x: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n ( \n <span style="color: #339933">\n ++x \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Post-decrement Value of x--: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n -- \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Pre-decrement Value of --x: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n ( \n <span style="color: #339933">\n --x \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output3

Shorthand Operators

Shorthand operators are binary operators normally used with the assignment operator. They aim to enable developers to write shorter code.

Syntax:

Operand operator= expression

//Expression here can be a single value or a set of variables.

OperatorSign Operator Name Operator Type Operation Performed Example
+= Addition Assignment Binary Shorthand notation for the addition of two operands. a+=b
-= SubtractionAssignment Binary Shorthand notation for the Subtraction of two operands. a-=b
*= Multiplication Assignment Binary Notation for the division of two operands. a*=b
/= Division Assignment Binary Shorthand notation for the division of two operands. a/=b
%= Modulus Assignment Binary Shorthand notation for the modulus of two operands. a%=b

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class IncDec_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n int x \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 2 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Original Value of x: " \n <span style="color: #339933">\n +x \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Post-increment Value of x++: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n ++ \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Pre-increment Value of ++x: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n ( \n <span style="color: #339933">\n ++x \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Post-decrement Value of x--: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n -- \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Pre-decrement Value of --x: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n ( \n <span style="color: #339933">\n --x \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #000000;font-weight: bold">\n class Shorthand_Operator \n <span style="color: #009900">\n { \n
\n <span style="color: #000000;font-weight: bold">\n public \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n int x \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 6 \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n int y \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 7 \n <span style="color: #339933">\n ; \n
x \n <span style="color: #339933">\n += \n <span style="color: #cc66cc">\n 5 \n <span style="color: #339933">\n ; \n
y \n <span style="color: #339933">\n -= \n <span style="color: #cc66cc">\n 2 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Value of x: " \n <span style="color: #339933">\n +x \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Value of y: " \n <span style="color: #339933">\n +y \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
x \n <span style="color: #339933">\n *= x \n <span style="color: #339933">\n +y \n <span style="color: #339933">\n ; \n
y \n <span style="color: #339933">\n /= x \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "New Value of x: " \n <span style="color: #339933">\n +x \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "New Value of y: " \n <span style="color: #339933">\n +y \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n <p><strong><span style="text-decoration: underline">Output:</span></strong></p> \n <figure class="wp-block-image"> \n <img src="https://lh5.googleusercontent.com/Qu7rPLGoGwtBFFGeke-HNEuA5rPrHSOb61qfRno2glJY_o0yI8Oa9PcHH13DCQr4j4q9oSTwi-m_uzvd5FyPNlC-8uG7nHQuIKpHN2i3sAzjR8SJeAIO59-Quvz7wp4A0TqDUJjCvVDSIcJYAA" alt="output4"> \n </figure> \n <h4><strong>Read: <a href="https://www.shiksha.com/online-courses/articles/data-types-in-java-primitive-and-non-primitive-data-types/" target="_blank" rel="noreferrer noopener">Data Types in Java</a></strong></h4> \n <h2 id="s3"><strong>Relational Operators</strong></h2> \n <p>Relational operators are binary operators since they work upon operators. They are further divided into 2 categories:</p> \n <ul> \n <li>Comparison Operators</li> \n <li>Equality Operators</li> \n </ul> \n <h3 id="s3s1"><strong>Comparison Operators</strong></h3> \n <p>Comparison operators are the binary relational operators in java. It includes <, <=, >, >=. They are normally used in expressions to evaluate their values.</p> \n <figure class="wp-block-table"> \n <table> \n <tbody> \n <tr> \n <th><strong>Operator</strong><strong>Sign</strong></th> \n <th><strong>Operator Name</strong></th> \n <th><strong>Operator Type</strong></th> \n <th><strong>Operation Performed</strong></th> \n <th><strong>Example</strong></th> \n </tr> \n <tr> \n <td>></td> \n <td>Greater than</td> \n <td>Binary</td> \n <td>Returns true if the left-hand operand value is greater than the right-hand operand value.</td> \n <td>a>b</td> \n </tr> \n <tr> \n <td>>=</td> \n <td>Greater than equal to</td> \n <td>Binary</td> \n <td>Returns True if the left-hand operand value is greater than or equal to the right-hand operand value.</td> \n <td>a>=b</td> \n </tr> \n <tr> \n <td><</td> \n <td>Less than</td> \n <td>Binary</td> \n <td>Returns true if the left-hand operand value is lesser than the right-hand operand value.</td> \n <td>a<b</td> \n </tr> \n <tr> \n <td><=</td> \n <td>Less than equal to</td> \n <td>Binary</td> \n <td>Returns True if the left-hand operand value is lesser than or equal to the right-hand operand value.</td> \n <td>a<=b</td> \n </tr> \n </tbody> \n </table> \n </figure> \n <p><strong><span style="text-decoration: underline">Example:</span></strong></p> \n
Copy code

Output:

output5

Equality Operators

The == and != operators come under the category of equality operators as they are used to check the equality between two operands. These operators are relational as well logical operators.

OperatorSign Operator Name Operator Type Operation Performed Example
== Equal equal to Binary Returns True if the left-hand operand value is equal to the right-hand operand value. a==b
!= Not Equal to Binary Returns True if the left-hand operand value is  NOT equal to the right-hand operand value. a!=b

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class Relational_Comparison_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n int x \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 6 \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n int y \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 10 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Is x>y: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n >y \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Is x<y: " \n <span style="color: #339933">\n + \n <span style="color: #009900">\n (x \n <span style="color: #339933">\n <y \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n <span style="color: #000000;font-weight: bold">\n class Relational_Equality_Operator \n <span style="color: #009900">\n { \n
\n <span style="color: #000000;font-weight: bold">\n public \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n char ch \n <span style="color: #339933">\n = \n <span style="color: #0000ff">\n 'a' \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n int i \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 97 \n <span style="color: #339933">\n ; \n
\n <span style="color: #000000;font-weight: bold">\n if \n <span style="color: #009900">\n (ch \n <span style="color: #339933">\n != i \n <span style="color: #009900">\n ) \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Not Equal" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #000000;font-weight: bold">\n else \n
\n <span style="color: #009900">\n { \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Equal" \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #000000;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #009900"> \n </span style="color: #339933"> \n </span style="color: #0000ff"> \n </span style="color: #009900"> \n </span style="color: #006633"> \n </span style="color: #006633"> \n </span style="color: #003399"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #339933"> \n </span style="color: #cc66cc"> \n </span style="color: #339933"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #009900"> \n </span style="color: #003399"> \n </span style="color: #009900"> \n </span style="color: #000066;font-weight: bold"> \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #000000;font-weight: bold">\n \n </span style="color: #009900">\n \n </span style="color: #000000;font-weight: bold">\n \n </pre class="java" style="font-family:monospace">
Copy code

Output:

output6

Logical Operators

Boolean logical operators are the binary operators that work upon boolean operands.

OperatorSign Operator Name Operator Type Operation Performed Example
& Logical AND Binary Performs the logical AND on the two boolean operands. Returns true only if both the operands are true else false. a&b
| Logical OR Binary Performs the logical OR on the two boolean operands. Returns true if either of the two operands are true else false. a|b
! Logical NOT Unary Performs the logical NOT on the boolean operand. Returns the inverse value of the operand. !b
^ Logical XOR Binary Performs the logical XOR on the two boolean operands. Returns true only if the two operands are not identical. a^b
== Equal equal to Binary Returns True if the left-hand operand value is equal to the right-hand operand value. a==b
!= Not Equal to Binary Returns True if the left-hand operand value is  NOT equal to the right-hand operand value. a!=b
&& Conditional AND Binary Returns True if both the clauses in the expression are true. Does not evaluate the second clause in the expression if the first clause is false. (a|b)&&(b|b)
|| Conditional OR Binary Returns True if either of the clauses in the expression is true. Does not evaluate the second clause in the expression if the first clause is true. (a&b)||(b&c)

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class Logical_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n boolean a \n <span style="color: #339933">\n = \n <span style="color: #000066;font-weight: bold">\n true \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean b \n <span style="color: #339933">\n = \n <span style="color: #000066;font-weight: bold">\n false \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean c \n <span style="color: #339933">\n = \n <span style="color: #000066;font-weight: bold">\n true \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "a= " \n <span style="color: #339933">\n +a \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "b= " \n <span style="color: #339933">\n +b \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "c= " \n <span style="color: #339933">\n +c \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_AND \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n &b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "a&b= " \n <span style="color: #339933">\n +result_AND \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_OR \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n |b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "a|b= " \n <span style="color: #339933">\n +result_OR \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_NOT \n <span style="color: #339933">\n = \n <span style="color: #339933">\n !b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "!b= " \n <span style="color: #339933">\n +result_NOT \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_XOR \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n ^b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "a^b= " \n <span style="color: #339933">\n +result_XOR \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_Cond_AND \n <span style="color: #339933">\n = \n <span style="color: #009900">\n (a \n <span style="color: #339933">\n |b \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n && \n <span style="color: #009900">\n (b \n <span style="color: #339933">\n |c \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "(a|b)&&(b|c)= " \n <span style="color: #339933">\n +result_Cond_AND \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_Cond_OR \n <span style="color: #339933">\n = \n <span style="color: #009900">\n (a \n <span style="color: #339933">\n &b \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n || \n <span style="color: #009900">\n (b \n <span style="color: #339933">\n &c \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "(a&b)||(b&c)= " \n <span style="color: #339933">\n +result_Cond_OR \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_Equal \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n ==b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "a==b= " \n <span style="color: #339933">\n +result_Equal \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n boolean result_Not_Equal \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n !=b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "a!=b= " \n <span style="color: #339933">\n +result_Not_Equal \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #009900">\n } \n
\n <span style="color: #009900">\n } \n <p><strong><span style="text-decoration: underline">Output:</span></strong></p> \n <figure class="wp-block-image"> \n <img src="https://lh4.googleusercontent.com/7gjsgUgEBMSd5FI4GGeLvjr5BrdhXnKnPsUCAy3XuwElvvIdqu2f_q1pIsLpwD1QxqU1bC5TsBNQJvdIrYuJXaZKFGPab5WNE5Dlcsk2T1QbxpnuQ0gRTuz_D5493OBAO3WSUQoHQuLTI4J3_A" alt="output7"> \n </figure> \n <h4><strong>Read: <a href="https://www.shiksha.com/online-courses/articles/java-hello-world-program/" target="_blank" rel="noreferrer noopener">Getting Started with Java Hello World Program</a></strong></h4> \n <h2 id="s5"><strong>Bitwise Operators</strong></h2> \n <p>These operators perform manipulations on the bits of the operands.</p> \n <h3 id="s5s1"><strong>Bitwise Logical Operators</strong></h3> \n <figure class="wp-block-table"> \n <table> \n <tbody> \n <tr> \n <th><strong>Operator</strong><strong>Sign</strong></th> \n <th><strong>Operator Name</strong></th> \n <th><strong>Operator Type</strong></th> \n <th><strong>Operation Performed</strong></th> \n <th><strong>Example</strong></th> \n </tr> \n <tr> \n <td>&</td> \n <td>Bitwise AND</td> \n <td>Binary</td> \n <td>Performs the Bitwise AND on the bits of two operands. Returns 1 if both the operands have the value 1.</td> \n <td>a&b</td> \n </tr> \n <tr> \n <td>|</td> \n <td>Bitwise OR</td> \n <td>Binary</td> \n <td>Performs the Bitwise OR on the bits of two operands. Returns 1 if either of the operands has the value 1.</td> \n <td>a|b</td> \n </tr> \n <tr> \n <td>~</td> \n <td>Bitwise NOT</td> \n <td>Unary</td> \n <td>Performs the Bitwise NOT on the bits of operands. Returns inverse of the value of the operand.</td> \n <td>~b</td> \n </tr> \n <tr> \n <td>^</td> \n <td>Bitwise XOR</td> \n <td>Binary</td> \n <td>Performs the Bitwise XOR on the bits of two operands. Returns 1 if the operands have dissimilar/different values.</td> \n <td>a^b</td> \n </tr> \n </tbody> \n </table> \n </figure> \n <p><strong><span style="text-decoration: underline">Example:</span></strong></p> \n
Copy code
java| class Bitwise_shift_Operator  {
     public  static  void main ( String [ ] args {
        int a  14 ;
        int b  8 ;
 
        int AND  = a &b ;
        System. out. println ( "Decimal of a&b: "  +AND ) ;
        System. out. println ( "Binary of a&b: "  + Integer. toBinaryString (AND ) ) ;
 
         int OR  = a |b ;
        System. out. println ( "Decimal of a|b: "  +OR ) ;
        System. out. println ( "Binary of a|b: "  + Integer. toBinaryString (OR ) ) ;
 
        int NOT  = ~a ;
        System. out. println ( "Decimal of ~a: "  +NOT ) ;
        System. out. println ( "Binary of ~a: "  + Integer. toBinaryString (NOT ) ) ;
 
        int XOR  = a ^b ;
        System. out. println ( "Decimal of a^b: "  +XOR ) ;
        System. out. println ( "Binary of a^b: "  + Integer. toBinaryString (XOR ) ) ;     
     }
}

Output:

output8
What is StringBuffer in Java?
What is StringBuffer in Java?
The below article goes through the explanation and implementation of the mutable string called StringBuffer in Java along with examples.
Understanding OOPs Concepts in Java
Understanding OOPs Concepts in Java
Object oriented programming (OOP) concepts are the fundamental pillars of programming. The article below explains the OOPs concept in Java. It covers introductions to OOPs, class, objects, inheritance, abstraction, encapsulation,...read more
Implementing Array in Java
Implementing Array in Java
Arrays in Java help to maintain elements of same datatype under a single variable name. The below article explains the implementation of array in java with examples.

Bitwise Shift Operators

OperatorSign Operator Name Operator Type Operation Performed Example
>> Right Shift Binary Shift bits in a pattern to the right, to the number of times specified in the expression. It maintains the sign of the number after shifting. a>>2
<< Left Shift Binary Shift bits in a pattern to the left, to the number of times specified in the expression. a<<2
>>> Unsigned Right Shift Binary Shift bits in a pattern to the right, to the number of times specified in the expression. It always stuffs a 0 in the leftmost position of the number irrespective of the sign of the number. a>>>2

Example:

 
\n \n <pre class="java" style="font-family:monospace">\n \n <span style="color: #000000;font-weight: bold">\n \n class Bitwise_shift_Operator \n \n <span style="color: #009900">\n \n {\n \n
\n \n <span style="color: #000000;font-weight: bold">\n \n public \n \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n int a \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 14 \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n int b \n <span style="color: #339933">\n = \n <span style="color: #cc66cc">\n 8 \n <span style="color: #339933">\n ; \n
\n
\n <span style="color: #000066;font-weight: bold">\n int AND \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n &b \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Decimal of a&b: " \n <span style="color: #339933">\n +AND \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Binary of a&b: " \n <span style="color: #339933">\n + \n <span style="color: #003399">\n Integer. \n <span style="color: #006633">\n toBinaryStclass Bitwise_shift_Operator \n <span style="color: #009900">\n { \n
\n <span style="color: #000000;font-weight: bold">\n public \n <span style="color: #000000;font-weight: bold">\n static \n <span style="color: #000066;font-weight: bold">\n void main \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n String \n <span style="color: #009900">\n [ \n <span style="color: #009900">\n ] args \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n { \n
\n <span style="color: #000066;font-weight: bold">\n int a \n <span style="color: #339933">\n = \n <span style="color: #339933">\n - \n <span style="color: #cc66cc">\n 2 \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n int result_leftshift \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n << \n <span style="color: #cc66cc">\n 1 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n print \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Before Left Shift: " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n Integer. \n <span style="color: #006633">\n toBinaryString \n <span style="color: #009900">\n (a \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n print \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "After Left Shift: " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n Integer. \n <span style="color: #006633">\n toBinaryString \n <span style="color: #009900">\n (result_leftshift \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n print \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Before Right Shift: " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n Integer. \n <span style="color: #006633">\n toBinaryString \n <span style="color: #009900">\n (a \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #000066;font-weight: bold">\n int result_rightshift \n <span style="color: #339933">\n = a \n <span style="color: #339933">\n >> \n <span style="color: #cc66cc">\n 1 \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n print \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "After Right Shift: " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n println \n <span style="color: #009900">\n ( \n <span style="color: #003399">\n Integer. \n <span style="color: #006633">\n toBinaryString \n <span style="color: #009900">\n (result_rightshift \n <span style="color: #009900">\n ) \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n
\n <span style="color: #003399">\n System. \n <span style="color: #006633">\n out. \n <span style="color: #006633">\n print \n <span style="color: #009900">\n ( \n <span style="color: #0000ff">\n "Before Unsigned Right Shift: " \n <span style="color: #009900">\n ) \n <span style="color: #339933">\n ; \n
\n