Arithmetic operations
Let's start with the simplest - with arithmetic operations. These are known to all 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 have 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 is an extra “tail” that is not divisible by 2 - one. This "tail" will be the result of the "remainder from division" operation. Java (as well as mathematics) implements comparison operations . They are also probably familiar to you from school:
- equals (
==
) - more (
>
) - less (
<
) - greater than or equal to (
>=
) - less than or equal (
<=
) - not equal (
!=
)
==
, not as a single character =
. The single character =
in Java is an assignment operator, when a number, string, or the value of another variable is assigned to a 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 data type: we expected to see boolean
, but got a number. This is because in brackets we have an assignment operation, not a comparison. x=y
The value y
(999) was assigned to the variable x
, and after that 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 2 numbers! :) Another feature of the assignment operation ( =
) is that it can be carried out “along the chain”:
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: the assignment is from right to left. This expression ( x = y = z
) will be executed step by step:
- y \u003d z, that is, y \u003d 256
- x = y, i.e. x = 256
Unary operations
They are called "unary" from the word "uno" - "one". They got this name because, unlike the previous ones, they are carried out over one number, and not over 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 unary minus two times. As a result, our number became negative at first, and then positive again!
-
Increment (
++
) and decrement (--
)An operation
++
increments a number by one, and an operation--
decrements it by the same one.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 a 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 increase x
by 1 and assign the 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? Also how it worked. To see 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, the postfix operation is called that because it is performed after the main expression. That is, in our case: int y = x++;
first it is executed y = x
(and the initial value of y will be assigned to the variable 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 should be remembered immediately so as not to make mistakes in a real program, in which all behavior can be turned upside down because of this :)
Combined operations
In addition, there are so-called combined operations in Java. 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 brevity, two characters are used in a row. It also works with combinations -=
, *=
, /=
and %=
.
Boolean operations
In addition to operations on numbers, in Java there are also 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
-
&&
- "AND" operator. Returnstrue
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.&&
In order to returntrue
, the operator needs all conditions to be true (as in the second line, for example). -
||
- "OR" operator. Returnstrue
when at least one of the operands is true.Here our previous example will already 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 it is enough for the “or” operator that the first part (100 > 10) is true.
GO TO FULL VERSION