OPERATORS IN JAVA
Operators represent actions that are performed on operands, giving a certain result.
Expressions are arbitrarily complex compositions of operands and operators.
Operators can be applied to one operand (unary operators) or to two operands (binary operators).
There is also one operator with three operands, which is ? : (explained later).
Expressions are arbitrarily complex compositions of operands and operators.
Operators can be applied to one operand (unary operators) or to two operands (binary operators).
There is also one operator with three operands, which is ? : (explained later).
ARITHMETIC OPERATORS IN JAVA
They are used for expressions with basic arithmetic operations. The table below provides an overview of arithmetic operators
The following table provides an overview of arithmetic operators.
Operator | Using | Description |
---|---|---|
+ | op1+op2 | Adds op1 and op2 |
- | op1-op2 | Subtracts op2 from op1 |
* | op1*op2 | Multiply op1 by op2 |
/ | op1/op2 | Divides op1 by op2 |
% | op1%op2 | Calculates the remainder of dividing op1 by op2 |
The following is an example of ArithmeticOperators in which two integer variables and two integers with double precision (double) are defined and various arithmetic operations are performed with them
Other calculation operations follow:
Running an application:
Remainder of dividing two numbers. The "%" operator (Division by module).
This operator is very often used to solve certain tasks in programming. For example. for testing divisibility. The "%" operator calculates the remainder of dividing two numbers.
For example, let's find the remainder of the following division:
For example, let's find the remainder of the following division:
A=7 / 3
Let's look at the well-known procedure for determining the remainder when dividing two numbers:
The result of the division in this example is 2, and the integer remainder of the division is 1.
This integer remainder would be obtained directly using the "%" operator:
This integer remainder would be obtained directly using the "%" operator:
A=7 % 3 = 1
To demonstrate the use of this operator, consider the following example:
Example 1: Setting the time in minutes and seconds
Example 1: Enter the time in seconds and determine how many minutes and how many seconds it actually is. So print the time in :mm:ss format.
Solving: If the user enters a time from e.g. 155s, that means the time should be written as 2min: 35s.
Considering that one minute has 60s, we can get the number of minutes in the value of 155 by dividing the integer by 60, which in this case is equal to 2.
Considering that one minute has 60s, we can get the number of minutes in the value of 155 by dividing the integer by 60, which in this case is equal to 2.
int time = 155, minutes;
minutes=time / 60;// 155 / 60 = 2(rounded to 2 because whole numbers are divided)
minutes=time / 60;// 155 / 60 = 2(rounded to 2 because whole numbers are divided)
To determine the remaining time in seconds, you need to find the remainder of dividing by 60:
int time = 155, minutes;
int seconds;
minutes = time / 60;// 155 / 60=2
seconds=time % 60;// 155 % 60 = 35
int seconds;
minutes = time / 60;// 155 / 60=2
seconds=time % 60;// 155 % 60 = 35
The remainder could be obtained in another way, by subtraction, but this example shows how to use the "%" operator.
The complete code with user time input is shown below:
The complete code with user time input is shown below:
Example 2: The hundreds digit
Example 2: Write a program that extracts the hundreds digit from the decimal notation of a number.
Instructions: The user should first enter the number B. Let it be e.g. B =789. In order to extract individual digits from the number B, we will introduce three additional variables for the hundreds, tens and ones digits.
int B= 789;
int s,d,j; // hundreds, tens and units digits. Since the 789=7*100 + 8*10 + 9, s=7, d=8 and j=9
int s,d,j; // hundreds, tens and units digits. Since the 789=7*100 + 8*10 + 9, s=7, d=8 and j=9
In order to calculate these digits, using the number B, we will use the operators "/" and "%". If we want to get the number 9, we will divide by 10:
789 / 10 = 9
To get the tens digit, "8" in this example, we perform two consecutive operations: division by 10 and then the remainder of division by 10
(789 / 10) % 10 = (78) % 10 = 8
It should be borne in mind that the division is performed with integer constants (int), so the result of the division is a whole number, i.e. instead of 78.9, only 78 will be obtained, which is the first smaller integer.
We can get the units digit in a similar way:
(789 / 10) / 10 = (78) / 10 = 7
So, to calculate the tens digit in the code, you should write:
int B= 789;
int s,d,j;
d = B / 10 % 10;
int s,d,j;
d = B / 10 % 10;
The rest of the task is left to the reader to solve independently.
Finally, the number N should be determined, without the hundreds digit. One way would be:
Finally, the number N should be determined, without the hundreds digit. One way would be:
int N;
N = d * 10 + j;//N= 8 * 10 + 9 = 89
N = d * 10 + j;//N= 8 * 10 + 9 = 89
Data types for operands
When an integer and a real number are used together as operands in an arithmetic operation, the result is a real number. The integer is implicitly converted to a real number before the actual calculation. The following table shows the types of data returned from arithmetic operations, based on the types of operands. The necessary conversions are performed before the operation is performed.
The data type for the result | Data types for operands |
---|---|
long | None of the operands is float or double (integer arithmetic), a at least one of the operators is of type long |
int | None of the operands are floats or doubles (integer arithmetic). None of the operands are long. |
double | At least one operand is of type double |
float | At least one operand is of float type. None are of type double |
There are also two operands that allow abbreviated computation. These are the operator ++ (increment) which increases the operand by 1 and the operator --(decrement) which decreases the operand by 1. Both operators can appear before the operand (prefix) and after the operand (postfix). With the prefix version, ++op/--op, the first operand is incremented by 1, so that result is used further in the expressions. In the postfix version, the first operand is applied in the expression (the old value), and only then is that value changed.
OPERATOR EXAMPLE++/--
Set the values of the variables for two types of fruit trees and on this example show the difference in the use of the operator ++/-- as a prefix and a suffix
Posle pokretanja programa:
In the case when the operator "++" is used as a prefix, the number of oranges, which was initially 5, first increased to 6, so the total number of fruit trees is 10 + 6 = 16. In the second case, when the operator "++ ” used as a suffix in the expression, first the total number of fruit trees was calculated with the original value of the number of apples 5, therefore, 10 + 5 = 15, and only after that the number of apples increased to 6. When the number of apples decreased, ie. If the operator "- - " was used, the logic would be the same.
RELATIONAL AND CONDITIONAL OPERATORS
The relational operator compares two values and determines the value between them. For example, != returns true if the two operands are not equal. The following table lists the relational operators:
Operator | Using | Return true if |
---|---|---|
> | op1>op2 | op1 veće od op2 |
>= | op1 >= op2 | op2 greather than or equal to op1 |
< | op1 < op2 | op1 is less than op2 |
<= | op1 <= op2 | op1 is less than or equal to op2 |
== | op1 == op2 | op1 is equal to op2 |
!= | op1 != op2 | op1 is different of op2 |
In the following example, we defined three integers and compared them using relational operators.
EXAMPLE OF RELATIONAL OPERATORS
Set the values of three integer variables and show different examples of the use of relational operators
Here's what the output of this program looks like:
Relational operators are often used together with logical operators, resulting in complex expressions. In Java, there are the following logical operators:
Operator | Using | Return true if |
---|---|---|
&& | op1 && op2 | both op1 and op2 have the value true. op2 is calculated only if necessary |
|| | op1 || op2 | either op1 or op2 has the value true. op2 is calculated only if necessary |
! | !op | op has the value false |
& | op1 & op2 | both op1 and op2 have the value true. Both op1 and op2 are always calculated |
| | op1 | op2 | Either op1 or op2 is true. Both op1 and op2 are always calculated. |
^ | op1 ^ op2 | if op1 and op2 are different, i.e. if one has the value true, but not both |
What is the difference between && and & operators? The difference is in the speed of program execution. With the & operator, the value of the second operator is always calculated, while with the && operator, the value of the first operand is calculated, and if that is enough to calculate the value of the entire expression, the second operand is not calculated.
ASSIGNMENT OPERATOR
The basic assignment operator is the "=" operator, which assigns one value to another. In Java, there are also assignment operators that perform multiple operations at once. Suppose you want to sum the value of a variable with some number and assign the result to the same variable. You would write: i = i + 2; In short, this can be written using the += operator as follows: i += 2; The previous two expressions are equivalent. The following table lists some of the operators of this type:
Operator | Using | Equivalent to: |
---|---|---|
+= | op1 += op2 | op1 = op1 + op2 |
-= | op1 -= op2 | op1 = op1 - op2 |
*= | op1 *= op2 | op1 = op1 * op2 |
/= | op1 /= op2 | op1 = op1 / op2 |
%= | op1 %= op2 | op1 = op1 % op2 |
&= | op1 &= op2 | op1 = op1 & op2 |
|= | op1 |= op2 | op1 = op1 | op2 |
^= | op1 ^= op2 | op1 = op1 ^ op2 |
OTHER OPERATORS
The following table shows the other operators that exist in Java
Operator | Operator description |
---|---|
[] | Indexing |
() | function call |
. | access to a member (element) of the object |
-> | the -> separates parameters and lambda expression body. |
Operator ?:
This operator returns op2 if op1 is true or op3 if op1 is false.
op1 ? op2 : op3
Suppose we have two integer variables of type int, myYear and yourYear. We want to assign the larger of these two values to the third variable, older. Here's what the statement looks like: older = yourYears > myYears ? yourYear : myYear The first argument of this operator is the logical expression yourYear > myYear. If that expression has the value true, the older variable gets the value of yourYear, and if the first expression evaluates to false, the older variable gets the value of myYear.
Operator []
This operator is used to declare arrays and to access array elements. float [] realArray = new float[10]; This expression declares an array of real numbers with ten elements. You can access the seventh member of the array as follows: realArray[6]
Operator .
This operator is used to access members of a class. More about him when we talk about classes.
Operator ()
It is used when declaring and calling class methods. More about him when we talk about classes.
Operator (tip)
The value is converted to the specified type. For example. for (double) a, the value of a is converted to type double.
Operator new
Used to create a new object or array. E.g. Integer a = new Integer(10); In this example, a new object of class Integer is created.
Operator instanceof
This operator tests whether the first operator represents an instance of the class given by the second operator. op1 instanceof op2 op1 must be an object and op2 must be a class name. An object is an instance of a class if it is an instance of that class or of a class derived from it.
This operator returns op2 if op1 is true or op3 if op1 is false.
op1 ? op2 : op3
Suppose we have two integer variables of type int, myYear and yourYear. We want to assign the larger of these two values to the third variable, older. Here's what the statement looks like: older = yourYears > myYears ? yourYear : myYear The first argument of this operator is the logical expression yourYear > myYear. If that expression has the value true, the older variable gets the value of yourYear, and if the first expression evaluates to false, the older variable gets the value of myYear.
Operator []
This operator is used to declare arrays and to access array elements. float [] realArray = new float[10]; This expression declares an array of real numbers with ten elements. You can access the seventh member of the array as follows: realArray[6]
Operator .
This operator is used to access members of a class. More about him when we talk about classes.
Operator ()
It is used when declaring and calling class methods. More about him when we talk about classes.
Operator (tip)
The value is converted to the specified type. For example. for (double) a, the value of a is converted to type double.
Operator new
Used to create a new object or array. E.g. Integer a = new Integer(10); In this example, a new object of class Integer is created.
Operator instanceof
This operator tests whether the first operator represents an instance of the class given by the second operator. op1 instanceof op2 op1 must be an object and op2 must be a class name. An object is an instance of a class if it is an instance of that class or of a class derived from it.
PRIORITET OPERATORA
Ako se u izrazu nađe više operatora oni se izvršavaju određenim redosledom. U tabeli koja sledi je dat prioritet operatora. Operatori sa najvišim prioritetom su navedeni prvi, a zatim slede oni sa nižim prioritetom. Ako želite da promenite prioritet operatora, treba da koristite zagrade.
Prioritet | Broj operanada | Smer grupisanja: | Operatori: |
---|---|---|---|
15 | 2 | -> | [] () . -> |
14 | 1 | -> | ! ~ ++ -- + - * & (tip) sizeof |
13 | 2 | -> | * / % |
12 | 2 | -> | + - |
11 | 2 | -> | << >> |
10 | 2 | -> | < <= > >= |
9 | 2 | -> | == != |
8 | 2 | -> | & |
7 | 2 | -> | ^ |
6 | 2 | -> | | |
5 | 2 | -> | && |
4 | 2 | -> | || |
3 | 2 | -> | ?: |
2 | 2 | -> | = += -= *= /= %= &= ^= |= <<= >>= |
1 | 2 | -> | , |
Sledeće
Grananje u programu >| |
Srodni članci
Operatori u C/C++ jeziku
Petlje u programskom jeziku JAVA
Kreiranje web sajta
Grafika u Javi
ASP.NET Core web aplikacije
Petlje u programskom jeziku JAVA
Kreiranje web sajta
Grafika u Javi
ASP.NET Core web aplikacije