Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Operators

Java Operators

Operators in Java are employed to execute operations on variables and values.

In the following example, we utilize the + operator to sum two values:

Example

int x = 100 + 50;
While the + operator is commonly employed to sum two values as demonstrated above, 
it can also combine a variable with a value or another variable.

Example

int sum1 = 100 + 50; // 150 (100 + 50)

int sum2 = sum1 + 250; // 400 (150 + 250)

int sum3 = sum2 + sum2; // 800 (400 + 400)

In Java, operators are categorized into the following groups:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Bitwise operatorsrephrase

Arithmetic Operators

Arithmetic operators facilitate common mathematical operations.

Operator

Name

Description

Example

 

+

Addition

Performs addition on two values

x + y

 

Subtraction

 Performs subtraction by deducting one value from another.

x – y

 

*

Multiplication

Performs multiplication by multiplying two values together.

x * y

 

/

Division

Performs division by dividing one value by another.

x / y

 

%

Modulus

Calculates the remainder after division.

x % y

 

++

Increment

 Increments the value of a variable by 1.

++x

 

Decrement

Decrement the value of a variable by 1

–x

 

Java Assignment Operators

Assignment operators are employed to assign values to variables.

In the following example, the assignment operator (=) is used to assign the value 10 to a variable named x:

Example

int x = 10;

The addition assignment operator (+=) adds a value to a variable and assigns the result back to the variable.

Example

int x = 10;

+= 5;

A comprehensive list of all assignment operators:

Operator

Example

Same As                 

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x – 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

&=

x &= 3

x = x & 3

|=

x |= 3

x = x | 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3

Java Comparison Operators

Comparison operators are utilized to compare two values or variables. This aspect is crucial in programming as it aids in finding solutions and making decisions.

The outcome of a comparison operation is either true or false, known as Boolean values. You’ll delve deeper into these concepts in the Booleans and If..Else chapter.

In this example, we utilize the greater than operator (>) to determine if 5 is greater than 3.

Example

int x = 5;
int y = 3;
System.out.println(x > y); // returns true, because 5 is higher than 3
Operator Name Example
== Equal to x==y
!= Not equal x!=y
Greater than x>y
Less than x<y
>= Greater than or equal to x>=y
<= Less than or equal to x<=y

Java Logical Operators

You can also assess true or false conditions using logical operators.

Logical operators help establish the logical relationship between variables or values.

Operator

Name

Description

Example

&&

Logical and

Returns true if both conditions are true

x < 5 &&  x < 10

||

Logical or

Returns true if any one of the condition is true

x < 5 || x < 4

!

Logical not

Reverses the result, returning false if the original result is true

!(x < 5 && x < 10)