Arithmetic operations
Let's start with the simplest thing - with arithmetic operations. These are the well-known addition (+ sign), subtraction (-), multiplication (*) and division (/).public class Main {
public static void main(String[] args) {
int x = 999;
int y = 33;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
}
}
Console output:
1032
966
32967
30
You've already used all this. You can add an operation to them %
- the remainder of the division.
public class Main {
public static void main(String[] args) {
int x = 33;
int y = 33%2;
System.out.println(y);
}
}
Console output:
1
In this example, we divide 33 by 2. As a result, we get 16 and there remains an extra “tail” that is not divisible by 2 - one. This “tail” will be the result of the “remainder of division” operation. Java (as well as mathematics) implements comparison operators . You probably also know them from school:
- equals (
==
) - more (
>
) - less (
<
) - greater than or equal to (
>=
) - less than or equal to (
<=
) - not equal (
!=
)
==
, and not with one sign =
. The unit sign =
in Java is an assignment operator, where a variable is assigned a number, string, or the value of another variable.
public class Main {
public static void main(String[] args) {
int x = 33;
int y = 999;
System.out.println(x=y);// expect false to be printed to the console
}
}
Console output:
999
Oops! This is clearly not the result we expected. This is a completely different type of data: we expected to see boolean
, but got a number. This is because in parentheses we have an assignment operation, not a comparison. x=y
The value y
(999) was assigned to the variable x
, and then we printed it x
to the console. Correct option:
public class Main {
public static void main(String[] args) {
int x = 33;
int y = 999;
System.out.println(x==y);
}
}
Console output:
false
Now we have correctly compared the 2 numbers! :) Another feature of the assignment operation ( =
) is that it can be done “chained”:
public class Main {
public static void main(String[] args) {
int x = 999;
int y = 33;
int z = 256;
x = y = z;
System.out.println(x);
}
}
Console output:
256
Remember: assignment is done from right to left. This expression ( x = y = z
) will be executed in steps:
- y = z, that is, y = 256
- x = y, that is x = 256
Unary operations
They are called “unary” from the word “uno” - “one”. They received this name because, unlike the previous ones, they are carried out on one number, and not on several. These include:-
Unary minus. It reverses the sign of the number.
public class Main { public static void main(String[] args) { int x = 999; // change the sign for the first time x = -x; System.out.println(x); // change the sign a second time x= -x; System.out.println(x); } }
Console output:
-999 999
We used the unary minus twice. As a result, our number became negative at first, and then positive again!
-
Increment (
++
) and decrement (--
)An operation
++
increases a number by one, and an operation--
decreases it by the same unit.public class Main { public static void main(String[] args) { int x = 999; x++; System.out.println(x); x--; System.out.println(x); } }
Console output:
1000 999
x++
- postfix notation ++x
- prefix notation What is the fundamental difference if you put pluses and minuses before or after the number? Let's see in an example:
public class Main {
public static void main(String[] args) {
int x = 999;
int y = x++;
System.out.println(y);
}
}
Console output:
999
Is there something wrong! We wanted to increment x
by 1 and assign a new value to the variable y
. That is, y should be equal to 1000. But we have a different result - 999. It turns out that it x
was not increased, and the increment operation did not work? How it worked. To verify this, try printing x to the console at the end :)
public class Main {
public static void main(String[] args) {
int x = 999;
int y = x++;
System.out.println(y);
System.out.println(x);
}
}
Console output:
999
1000
In fact, that's why the postfix operation is called that way: it is performed after the main expression. That is, in our case: int y = x++;
first it is executed y = x
(and the variable y will be assigned the initial value x
), and only then x++
. What if we are not satisfied with this behavior? You need to use the prefix notation:
public class Main {
public static void main(String[] args) {
int x = 999;
int y = ++x;
System.out.println(y);
}
}
In this case, it will work first ++x
and only after that. y = x;
This difference is worth remembering right away so as not to make mistakes in a real program, in which all behavior can turn upside down because of this :)
Combined operations
In addition, in Java there are so-called combined operations. They use a combination of two operations:- Assignment
- Arithmetic operation
+=
-=
*=
/=
%=
public class Main {
public static void main(String[] args) {
int x = 999;
int y = 33;
x += y;
System.out.println(x);
}
}
Console output:
1032
x += y
means x = x + y
. Just for the sake of brevity, two characters in a row are used. This also works with combinations of -=
, *=
, /=
and %=
.
Logical operations
In addition to operations on numbers, Java also has operations on Boolean variables -true
and false
. These operations are performed using logical operators
-
!
— “NOT” operator. Reverses the value of a boolean variablepublic class Main { public static void main(String[] args) { boolean x = true; System.out.println(!x); } }
Console output:
false
-
&&
— operator “AND”. Returns a valuetrue
only if both operands aretrue
.public class Main { public static void main(String[] args) { System.out.println(100 > 10 && 100 > 200); System.out.println(100 > 50 && 100 >= 100); } }
Console output:
false true
The result of the first operation is
false
, since one of the conditions is false, namely 100 > 200. The operator requires all conditions to be true (as in the second line, for example)&&
in order to return .true
-
||
— “OR” operator. Returnstrue
when at least one of the operands is true.Here our previous example will work differently:
public class Main { public static void main(String[] args) { System.out.println(100 > 10 || 100 > 200); } }
Console output:
true
The expression 100 > 200 is still false, but the “or” operator is quite satisfied that the first part (100 > 10) is true.
GO TO FULL VERSION