[Go to site: main page, start]

0% found this document useful (0 votes)
12 views10 pages

Java Operators

The document provides an overview of Java operators, which are special symbols used to perform operations on variables and values. It categorizes operators into seven types: Arithmetic, Relational, Logical, Assignment, Unary, Bitwise, and Ternary, each with specific functionalities and examples. The document includes code snippets demonstrating the use of these operators in Java programming.

Uploaded by

ordinary0360
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Java Operators

The document provides an overview of Java operators, which are special symbols used to perform operations on variables and values. It categorizes operators into seven types: Arithmetic, Relational, Logical, Assignment, Unary, Bitwise, and Ternary, each with specific functionalities and examples. The document includes code snippets demonstrating the use of these operators in Java programming.

Uploaded by

ordinary0360
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit-2

Topic: Java Operators

Java operators are special symbols that perform operations on variables and
values. They are used to manipulate data and variables in expressions. Java
provides a wide array of operators categorized into several types based on their
functionality.

Types of Java Operators:

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

1. Arithmetic Operators:

Arithmetic operators are used for performing mathematical calculations such as


addition, subtraction, multiplication, and division. Arithmetic Operators are the
binary operators which require two operands. The result of arithmetic operations
depends upon type of operand used.

Example
public class Arithmetic_Operator
{
public static void main (String[] args) {
int a = 100;
byte b = 20;
[Link](a + b);
[Link](a - b);
[Link](a * b);
[Link](a/b);
[Link](a % b);

}
}

Output:
120
80
2000
5
0

2. Relational (Comparison) Operators

Relational Operators are used to establish the relation between two expressions
or it can be said that by using this relation operator we can form a relational
expression. The result of relational expression always returns some Boolean
values, that is either true or false.
Example:
public class RelationalOperator {
public static void main(String[] args)
{
int a = 20;
int b = 10;
[Link](a > b);
[Link](a < b);
[Link](a >= b);
[Link](a <= b);
[Link](a == b);
[Link](a != b);
}
}

Output
true
false
true
false
false
true
3. Logical Operators:

Logical operators are used to perform logical operations on boolean [Link]


operators are used to combine multiple conditions.

The following table lists the logical operators −

Assume Boolean variables A holds true and variable B holds false, then −

Operator Description Example

Called Logical AND operator. If both the operands


&& (logical and) (A && B) is false
are non-zero, then the condition becomes true.

Called Logical OR Operator. If any of the two


|| (logical or) operands are non-zero, then the condition becomes (A || B) is true
true.

Called Logical NOT Operator. Use to reverses the


! (logical not) logical state of its operand. If a condition is true !(A && B) is true
then Logical NOT operator will make false.

Example
class LogicalOperator
{
public static void main(String[] args) {
boolean cond1 = true, cond2 = false;
[Link] (cond1 && cond2); //
[Link] (cond1 || cond2); //
[Link] (!cond1); //
}
}
Output
false
true
false
[Link] Operators:

it is used to assign some value to a corresponding variable. Which means it stores


the Right-hand side value into the Left hand side variable. The left hand side
variable can be called destination and the right hand side variable can be called
source.

Operator Example Equivalent to

= a = b; a = b;

+= a += b; a = a + b;

-= a -= b; a = a - b;

*= a *= b; a = a * b;

/= a /= b; a = a / b;

%= a %= b; a = a % b;

Example:
class AssignmentOperator
{
public static void main(String[] args)
{

int num = 10;

num += 5;
[Link]("num += 5: " + num);
num -= 2;
[Link]("num -= 2: " + num);
num *= 3;
[Link]("num *= 3: " + num);
num /= 2;
[Link]("num /= 2: " + num);
num %= 4;
[Link]("num %= 4: " + num);
}
}
Output
15

13

39

19
3

5. Unary Operators:

Unary operators work on a single operand. For example, ++ is a unary operator


that increases the value of a variable by 1. That is, ++5 will return 6.

Different types of unary operators are:

Operator Meaning

+ Unary plus: not necessary to use since numbers are positive without using it

- Unary minus: inverts the sign of an expression

++ Increment operator: increments value by 1

-- Decrement operator: decrements value by 1

! Logical complement operator: inverts the value of a boolean


Example:

class UnaryOperator {
public static void main(String[] args) {
// Declare a variable
int a = 5;
[Link]("+a: " + (+a)); // Output: 5
[Link]("-a: " + (-a)); // Output: -5
[Link]("a++: " + (a++)); // Output: 5 (post-increment)
[Link]("After a++: " + a); // Output: 6
[Link]("a--: " + (a--)); // Output: 6 (post-decrement)
[Link]("After a--: " + a); // Output: 5
}
}

6. Bitwise Operators:

Bitwise Operator is used to work on individual bits which means it performs the
operation on a bit. Following are the bitwise operators.

1)Bitwise AND Operator [ &]


2)Bitwise OR Operator [ | ]
3)Bitwise XOR Operator [ ^ ]
4)Left-Shift Operator [ << ]
5)Right-Shift Operator [ >> ]
6)Negation or Tilde Operator [ ~ ]

1)Bitwise AND Operator [ &]

• It is a binary operator and is denoted by ‘&’.


• It performs operations bit by bit and if both bits are 1 then only it returns 1,
otherwise it returns 0.
2)Bitwise OR Operator [ | ]

It is a binary operator and it’s denoted by ‘|’.


It performs operations bit by bit and if both bits are 0 then only it returns 0,
otherwise it returns 1.

3)Bitwise XOR Operator [ ^ ]

• It is a binary operator and it's denoted by ^ .


• It is also called caret operator.
• It returns 0 if both bits are the same, otherwise it returns 1.
4)Left-Shift Operator [ << ]

• The bits in the left operand are shifted to the left by the number of places
specified in the right operand.
• It is denoted by the symbol <<

5)Right-Shift Operator [ << ]


The right shift operator shifts all the bits to the right. The empty space in the left
side is filled depending on the input number:

When an input number is negative, where the leftmost bit is 1, then the

empty spaces will be filled with 1
 When an input number is positive, where the leftmost bit is 0, then the
empty spaces will be filled with 0
6)Negation or Tilde Operator [ ~ ]
• The bitwise complement operator is a unary operator. It is denoted by ~.
• It performs Negation operation on the given operand.

Example

class BitwiseOperator
{
public static void main(String[] args) {
int x = 5, y = 3;

[Link]("x & y: " + (x & y)); // Output: 1


[Link]("x | y: " + (x | y)); // Output: 7
[Link]("x ^ y: " + (x ^ y)); // Output: 6
[Link]("~x: " + (~x)); // Output: -6
[Link]("x << 1: " + (x << 1)); // Output: 10
[Link]("x >> 1: " + (x >> 1)); // Output: 2
}
}
[Link] Operator
The ternary operator is a compact alternative to the if-else statement. It evaluates
a condition and returns one of two values depending on whether the condition is
true or false. It is commonly used for:
 Conditional value assignment
 Simple decision-making logic
 Replacing short if-else blocks

Syntax
variable = Expression1 ? Expression2: Expression3

Example:

class Main {

public static void main (String[] args) {

int a = 10, b = 20;

int res = (a > b) ? a : b;

[Link](res);

Output:
20

You might also like