0% found this document useful (0 votes)
6 views

Operators

The document explains various operators in Java, including assignment, arithmetic, unary, equality, relational, logical, and the ternary operator. Each operator is defined with its purpose and examples of usage. It highlights how these operators are used to perform operations on variables and expressions.

Uploaded by

mswanderer2609
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Operators

The document explains various operators in Java, including assignment, arithmetic, unary, equality, relational, logical, and the ternary operator. Each operator is defined with its purpose and examples of usage. It highlights how these operators are used to perform operations on variables and expressions.

Uploaded by

mswanderer2609
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment Operator

Assignment operators are used in Java to assign values to variables. For example,

int age;

age = 5;

The assignment operator assigns the value on its right to the variable on its left. Here, 5 is assigned
to the variable age using = operator.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication etc.

Java Arithmetic Operators

Operator Meaning

+ Addition (also used for string concatenation)

- Subtraction Operator

* Multiplication Operator

/ Division Operator

% Remainder Operator

Unary Operators

Unary operator performs operation on only one operand.

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

Equality and Relational Operators

The equality and relational operators determines the relationship between two operands. It checks
if an operand is greater than, less than, equal to, not equal to and so on. Depending on the
relationship, it results to either true or false.

Java Equality and Relational Operators

Operator Description Example

== equal to 5 == 3 is evaluated to false

!= not equal to 5 != 3 is evaluated to true

> greater than 5 > 3 is evaluated to true

< less than 5 < 3 is evaluated to false

>= greater than or equal to 5 >= 5 is evaluated to true

<= less then or equal to 5 <= 5 is evaluated to true

Logical operators in Java

Operator Name ` Description

&& (Logical AND) Returns the value if both the conditions are true otherwise
returns zero.

II (Logical OR) Returns the value if even one condition is true

! returns true only if the variable given is not present

Ternary Operator

The conditional operator or ternary operator ?: is shorthand for if-then-else statement. The syntax
of conditional operator is:
variable = Expression ? expression1 : expression2

Here's how it works.

If the Expression is true, expression1 is assigned to variable.

If the Expression is false, expression2 is assigned to variable.

You might also like