EN
Java - operators
0 points
In this article, we would like to show you operators that are used to perform operations on variables and values in Java.
Operator | Name | Description | Example |
---|---|---|---|
+ | addition | adds together two values | a + b |
- | subtraction | subtracts the second value from the first one | a - b |
* | multiplication | multiplies two values | a * b |
/ | division | divides the first value by the second one | a / b |
% | modulus | returns the division remainder | a % b |
++ | increment | increases the value of a variable by 1 | a++ |
-- | decrement | decreases the value of a variable by 1 | a-- |
In this example, we use +
operator to sum two values.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 10 + 20;
5
System.out.println(result); // 30
6
}
7
}
Output:
xxxxxxxxxx
1
30
In this example, we use -
operator to subtract two values.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 20 - 10;
5
System.out.println(result); // 10
6
}
7
}
Output:
xxxxxxxxxx
1
10
In this example, we use *
operator to multiply two values.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 10 * 2;
5
System.out.println(result); // 20
6
}
7
}
Output:
xxxxxxxxxx
1
20
In this example, we use /
operator to divide the first value by the second one.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 20 / 2;
5
System.out.println(result); // 10
6
}
7
}
Output:
xxxxxxxxxx
1
10
In this example, we use %
operator to get the division remainder.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 10 % 3;
5
System.out.println(result); // 1 (because 10/3=3 and 1 remains)
6
}
7
}
Output:
xxxxxxxxxx
1
1
In this example, we use ++
operator to increment the result
value.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 10;
5
result++;
6
System.out.println(result); // 11
7
}
8
}
Output:
In this example, we use --
operator to decrement the result
value.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int result = 10;
5
result--;
6
System.out.println(result); // 9
7
}
8
}
Output:
xxxxxxxxxx
1
9