JAVA BASICS
Java operators
Operators perform actions on values and variables. They let you calculate, assign values, compare data, combine logical conditions and manipulate individual bits.
An operator in an expression
In the simple expression 100 + 50 the symbol + is the operator and the numbers are operands. The result can be assigned to a variable immediately or used in another expression.
int suma1 = 100 + 50; // 150
int suma2 = suma1 + 250; // 400
int suma3 = suma2 + suma2; // 800Java operators are commonly grouped into arithmetic, assignment, comparison, logical and bitwise operators.
Arithmetic operators
Arithmetic operators perform mathematical operations on numeric values.
| The operator | Operation | Example |
|---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Remainder | x % y |
++ | Increment by one | x++ |
-- | Decrement by one | x-- |
int x = 17;
int y = 5;
System.out.println(x + y); // 22
System.out.println(x - y); // 12
System.out.println(x * y); // 85
System.out.println(x / y); // 3
System.out.println(x % y); // 217 / 5 with two int operands returns 3, and the remainder can be obtained with 17 % 5.
int taskai = 10;
taskai++;
System.out.println(taskai); // 11
taskai--;
System.out.println(taskai); // 10Assignment operators
The operator = assigns the value on the right to the variable on the left. Compound operators perform an operation and store the result in the same variable in one step.
| The operator | Equivalent form |
|---|---|
= | x = 5 |
+= | x = x + y |
-= | x = x - y |
*= | x = x * y |
/= | x = x / y |
%= | x = x % y |
int taskai = 10;
taskai += 5; // the same as taskai = taskai + 5
taskai *= 2; // the same as taskai = taskai * 2
System.out.println(taskai); // 30Comparison operators
A comparison always produces the Boolean value true or false.
| The operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
int amzius = 20;
System.out.println(amzius == 20); // true
System.out.println(amzius != 20); // false
System.out.println(amzius > 18); // true
System.out.println(amzius <= 19); // false== and = perform different operations.= assigns a value, while == compares. To compare object contents, such as text, the method often used is equals().
Logical operators
Logical operators combine or change boolean expressions.
&&returnstruewhen both conditions are true.||returnstruewhen at least one condition is true.!reverses the Boolean value.
int amzius = 20;
boolean turiBilieta = true;
boolean galiIeiti = amzius >= 18 && turiBilieta;
boolean taikomaNuolaida = amzius < 18 || amzius >= 65;
boolean bilietoNera = !turiBilieta;
System.out.println(galiIeiti); // true&& and || use short-circuit evaluation. If the first condition determines the result, the second is not evaluated:
String vardas = null;
boolean tinkamas = vardas != null && vardas.length() >= 3;
System.out.println(tinkamas); // falseThe method length() is not called when vardas is null, preventing an error.
Bitwise operators
Bitwise operators work on the binary representation of integers. They are used for flags, protocols, low-level formats and efficient bit calculations.
| The operator | Operation |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise complement |
<< | Left shift |
>> | Signed right shift |
>>> | Unsigned right shift |
int a = 6; // in binary: 0110
int b = 3; // in binary: 0011
System.out.println(a & b); // 2 (0010)
System.out.println(a | b); // 7 (0111)
System.out.println(a ^ b); // 5 (0101)
System.out.println(a << 1); // 12 (1100)
System.out.println(a >> 1); // 3 (0011)Operator precedence
When an expression contains multiple operators, Java follows their precedence. Multiplication happens before addition, but parentheses can explicitly change the order:
int rezultatas1 = 2 + 3 * 4; // 14
int rezultatas2 = (2 + 3) * 4; // 20Even if you know the precedence table, parentheses often make a complex expression easier to read.