We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
2arii2023 11:13 Java Operators
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
Java Operators
[crv] Looe |
Java Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and
another variable:
Example
int sum1 = 100 + 50; // 158 (100 + 50)
int sum2 = sum1 + 250; // 400 (15@ + 250)
int sum3 = sum2 + sum2; // 808 (400 + 400)
hitps:wwwschools.comfavaljava_operators.asp 92arii2023 11:13
w
schools
Tutorials
Exercises ¥
ava Operators
Q 0
Services ¥
Java divides the operators into the following groups:
* Arithmetic operators
* Assignment operators
* Comparison operators
* Logical operators
* Bitwise operators
Arithmetic Operators
Log in
Arithmetic operators are used to perform common mathematical operations.
Operator Name
+
%
++
hitpsslvaw.wschools.comfaval
Addition
Subtraction
Multiplication
Division
Modulus
Increment
Decrement
a_operators.asp
Description
Adds together two values
Subtracts one value from
another
Multiplies two values
Divides one value by another
Returns the division
remainder
Increases the value of a
variable by 1
Decreases the value of a
variable by 1
Example Try it
x+y
x-y
x*y
x/y
x%y
+4x
2192arii2023 11:13 Java Operators
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
Java Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10”
to a variable called x:
Example
int x = 10;
n assignment operator (+= ) adds a value to a variable:
Example
int x = 10;
x4
A list of all assignment operators:
Operator Example Same As Try it
= x=5 x=5
+ xt=3 x=x+3
hitps:wwwschools.comfavaljava_operators.asp a92arii2023 11:13 Java Operators
w Tutorials Exercisesw Servicesey§ QO Login
%= x%=3 X=x%3
& x&=3 x=x&3
l= x|=3 x=x/3
A xA=3 X=xA3
>>= x>>=3 xX=x>>3
<<= x<<=3 x=x<<3
Java Comparison Operators
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make decisions.
The return value of a comparison is either true or false. These values are known
as Boolean values, and you will learn more about them in the Booleans and If..Else
chapter.
In the following example, we use the greater than operator (> ) to find out if 5 is
greater than 3:
Example
int x = 55
int y = 3;
System.out.printin(x > y); // returns true, because 5 is higher than 3
Operator Name Example Try it
Equal to x==y
hitps:wwwschools.comfavaljava_operators.asp 492arii2023 11:13 Java Operators
3
WW tutoriaisy Exercises» Servicessy§ Q 0
schools
< Less than x Greater than or equalto x >= y
< Less than or equal to x<=y
Java Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:
Operator Name Description
BB Logical and Returns true if both
statements are true
ll Logical or Returns true if one of the
statements is true
Logicalnot —_—Reverse the result, returns
false if the result is true
Exercise:
Multiply 10 with 5, and print the result.
system.out.println(1@ 5)3
hitps:wwwschools.comfavaljava_operators.asp
Log in
Example Try it
x<58&X<
10
x<5I[x<4
M(x < 5 8& x
< 10)
592arii2023 11:13 Java Operators
Tutorialsy Exercisesy Servicesy QO
schools
Start the Exercise
[