Java Operators
Operators are used to perform different operations on variables and values. In Java operators can be divided into the following groups.
- Arithmetic Operators
- Logical Operators
- Assignment Operators
- Comparison Operators
(1)Arithmetic Operators in Java Operators
Arithmetic operators are used to perform arithmetic operations on variables and data.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | It adds to values | x+y |
* | Multiplication | It multiplies to values | x*y |
- | Subtraction | It subtracts one value from other | x-y |
/ | Division | Divides on value by another one | x+y |
% | Modulus | Return the Divion Reminder | x%y |
+ Operator example
Explanation:
Code | Explanation |
---|---|
int x =12; | Declare variable name x with data type int and store 12 inside it |
int y =10; | Declare variable name y with data type int and store 10 inside it. |
int result= x+y; | Adding value present inside x and y and the sum will be saved inside variable name result having data type int. |
System.out.println(result); | System.out.println() is the function which take argument name result which is 22 and print on the console. |
* Operator example
Explanation:
Code | Explanation |
---|---|
int x =6 | Declare variable name x with data type int and store 6 inside it. |
int y =3; | Declare variable name y with data type int and store 3 inside it. |
int result= x*y; | Multiply value present inside x and y and the product will be saved inside variable name result having data type int. |
System.out.println(result); | System.out.println() is the function which take argument name result which is 18 and print on the console. |
- Operator example
Explanation:
Code | Explanation |
---|---|
int x =15 | Declare variable name x with data type int and store 15 inside it. |
int y =5; | Declare variable name y with data type int and store 5 inside it. |
int result= x-y; | Subtract y from x and result will be saved inside variable name result having data type int |
System.out.println(result); | System.out.println() is the function which take argument name result which is 10 and print on the console. |
/ Operator example
Explanation:
Code | Explanation |
---|---|
int x =20; | Declare variable name x with data type int and store 20 inside it. |
int y =5; | Declare variable name y with data type int and store 5 inside it. |
int result= x/y; | Divide x with y and result will be saved inside variable name result having data type int |
System.out.println(result); | System.out.println() is the function which take argument name result which is 4 and print on the console. |
% Operator example
Explanation:
Code | Explanation |
---|---|
int x =22; | Declare variable name x with data type int and store 22 inside it. |
int y =5; | Declare variable name y with data type int and store 5 inside it. |
int result= x%y; | Module x with y and reminder will be saved inside variable name result having data type int |
System.out.println(result); | System.out.println() is the function which take argument name result which is 2 and print on the console. |
(2)Logical Operators in Java Operators
Logical operators are used to determine the logic between variables or values:
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Returns true if both statements are true | y > 3 && y< 100 |
|| | Logical or | Returns true if one of the statements is true | x > 15 || x < 10 |
! | Logical not | Reverse the result, returns false if the result is true | !(x > 5 && x < 20) |
&& Operator
Explanation:
Code | Explanation |
---|---|
int x =10; | Declare variable name x with data type int and store 10 inside it. Declare variable name y with data type int and store 5 inside it. |
if(x>5 && x<15){ | Checking and condition, if x is greater than 5 and less than 15 which is true as x contain 10 which is greater then 5 and less then 15, so here && condition is true. |
System.out.println("True"); | As && condition is true so here True will be printed on the Screen. |
System.out.println("False"); | If && condition is false then False will be printed on the Screen. |
|| Operator
Code | Explanation |
---|---|
int x =10; | Declare variable name x with data type int and store 10 inside it. Declare variable name y with data type int and store 5 inside it. |
if(x>40 || x<5) | Checking logically or condition, if x is greater than 40 or x is less than 5 which is false as x contain 10 which is less than 40 and greater than 5, so here || condition is false. |
System.out.println("False"); | As || condition is false so here False will be printed on the Screen. |
System.out.println("True"); | If || condition is true then True will be printed on the Screen. |
! Operator
Explanation:
Code | Explanation |
---|---|
int x =50; | Declare variable name x with data type int and store 50 inside it.Declare variable name y with data type int and store 5 inside it. |
if(!(x>40)) | Checking logically not condition, if x is greater than 40 is true because x contain 50 now as we using logical not ! operator so it’s return false |
System.out.println("False"); | As ! condition return false because x>40 return true, so False will be printed on the screen. |
System.out.println("True"); | If x>40 return false than ! operator returns true and True will be printed on the screen. |
(3)Assignment Operators in Java Operators
Assignment operators are used to assign values to variables.
Operator | Example | Equal To |
---|---|---|
= | x=10 | x=10 |
+= | x+=4 | x=x+4 |
*= | x *=4 | x=x*4 |
-= | x-=6 | x=x-6 |
/= | x /=2 | x = x/2 |
%= | x%2 | x=x%2 |
= Operator
Explanation:
Code | Description |
---|---|
int x =50; | = Operate assign the value to x which is 50 |
System.out.println(x); | System.out.println() is the function which take argument name x which contain value 50 and print on the console. |
+= Operator
Explanation:
Code | Description |
---|---|
int x =50; | = Operate assign the value to x which is 50 |
x+=4 | x+=4 is equal to x=x+4 so as x=50 and x=50+4, so now x contains 54. |
System.out.println(x); | System.out.println() is the function which take argument name x which contain value 54 and print on the console. |
*= Operator
Explanation:
Code | Description |
---|---|
int x =50; | = Operate assign the value to x which is 50 |
x*=4 | x*=4 is equal to x=x*4 so as x=50 and x=50*4, so now x contains 200. |
System.out.println(x); | System.out.println() is the function which take argument name x which contain value 200 and print on the console. |
-= Operator
Explanation:
Code | Description |
---|---|
int x =50; | = Operate assign the value to x which is 50 |
x-=4 | x-=4 is equal to x=x-4 so as x=50 and x=50-4, so now x contains 46. |
System.out.println(x); | System.out.println() is the function which take argument name x which contain value 46 and print on the console. |
/= Operator
Explanation:
Code | Description |
---|---|
int x =50; | = Operate assign the value to x which is 50 |
x/=2 | x/=2 is equal to x=x/2 so as x=50 and x=50/2, so now x contains 25. |
System.out.println(x); | System.out.println() is the function which take argument name x which contain value 25 and print on the console. |
%= Operator
Explanation:
Code | Description |
---|---|
int x =23; | = Operate assign the value to x which is 23 |
x%=2 | x%=2 is equal to x=x%2 so as x=23 and x=23%2, so x contains 1 as the remainder is 1. |
System.out.println(x); | System.out.println() is the function which take argument name x which contain value 1 and print on the console. |
(4)Comparison Operators in Java Operators
Comparison operators are used to compare two values stored inside variables.
Following comparison operators are used in java language.
Operator | Name | Description | Example |
---|---|---|---|
== | Equal to | Checking value stored inside x variable is equal to value stored inside variable y. | x == y |
!= | Not equal to | Checking value stored inside x is not equal to value stored inside variable y. | x != y |
> | Greater than | Checking value stored inside x is greater than value stored inside variable y. | x > y |
< | Less than | Checking value stored inside x is less then value stored inside variable y. | x < y |
>= | Greater Than or equal to | Checking value stored inside x is equal to or greater than to value stored inside variable y. | x >= y |
<= | Less than or equal to | Checking value stored inside x is less than or equal to value stored inside variable y. | x <= y |
== Operator
Code | Description |
---|---|
int x =23; | Declare variable name x with data type int and store 23 inside it. |
If (x == 23) | == Operator checking whether value of x is equal to 23 or not which is true |
System.out.println(“True”); | System.out.println() function print the True on the screen because if( x == 23) condition is true. |
!= Operator
Explanation:
Code | Description |
---|---|
int x =30; | Declare variable name x with data type int and store 30 inside it. |
If (x != 25) | != Operator checking whether value of x is not equal to 25 which is true |
System.out.println(“True”); | System.out.println() function print the True on the screen because if( x != 25) condition is true. |
> Operator
Explanation:
Code | Description |
---|---|
int x =30; | Declare variable name x with data type int and store 30 inside it. |
If (x > 25) | > Operator checking whether value of x is greater than 25 or not which is true |
System.out.println(“True”); | System.out.println() function print the True on the screen because ( x > 25) condition is true. |
< Operator
Explanation:
Code | Description |
---|---|
int x =30; | Declare variable name x with data type int and store 30 inside it. |
If (x < 40) | < Operator checking whether value of x is less than 40 or not which is true |
System.out.println(“True”); | System.out.println() function print the True on the screen because ( x < 40) condition is true. |
>= Operator
Explanation:
Code | Description |
---|---|
int x =30; | Declare variable name x with data type int and store 30 inside it. |
If (x => 40) | => Operator checking whether value of x is greater than or equal to 40 which is true |
System.out.println(“True”); | System.out.println() function print the True on the screen because ( x >= 40) condition is true. |
<= Operator
Explanation:
Code | Description |
---|---|
int x =30; | Declare variable name x with data type int and store 30 inside it. |
If (x <= 40) | <= Operator checking whether value of x is less than or equal to 40 which is true |
System.out.println(“True”); | System.out.println() function print the True on the screen because ( x <= 40) condition is true. |
- Arithmetic Operators.
- Unary Operators.
- Assignment Operator.
- Relational Operators.
- Logical Operators.
- Ternary Operator.
- Bitwise Operators.
- Shift Operators.
Operators are used to perform different operations on variables and values.
- Arithmetic Operators
- Logical Operators
- Assignment Operators
- Comparison Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. it is like +,-*,/ and %
This query focuses on operators like ==
, !=
, <
, >
, <=
, and >=
used for comparing values and producing boolean results.
Learners often ask about &&
, ||
, and !
operators used to create complex boolean expressions and make decisions based on multiple conditions.
This query focuses on operators like =
, +=
, -=
, *=
, /=
, and %=
used to assign values and perform operations simultaneously.