Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Java provides a rich operator environment. Most of its operators can be divided into the
following four groups:
arithmetic,
bitwise
relational
logical
Arithmetic Operators
Java provides Arithmetic operators for addition, subtraction, multiplication, division, modulo (or
remainder), increment (or add 1), decrement (or subtract 1), and negation. An example program
is shown below that demonstrates the different arithmetic operators in java. Arithmetic operators
are used in mathematical expressions in the same way that they are used in algebra.
Task 01:
// Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
Task 02:
public class work2
{
public static void main(String args[])
{
int a = 60;
/* 60 = 0011 1100 */
int b = 13;
/* 13 = 0000 1101 */
int c = 0;
c = a & b;
/* 12 = 0000 1100 */
System.out.println("a & b = " + c );
c = a | b;
/* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b;
/* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = ~a;
/*-61 = 1100 0011 */
System.out.println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 */
System.out.println("a << 2 = " + c );
c = a >> 2;
/* 15 = 1111 */
Relational Operators
Relational operators in Java are used to compare 2 or more objects.
Task 03:
class work3
{
public static void main(String args[])
{
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
Task 04:
public class work4
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b));
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
}
}
Operator Precedence
The operators in the following table are listed according to precedence order. The closer to the
top of the table an operator appears, the higher its precedence. Operators with higher precedence
are evaluated before operators with relatively lower precedence. Operators on the same line have
equal precedence. When operators of equal precedence appear in the same expression, a rule
must govern which is evaluated first. All binary operators except for the assignment operators
are evaluated from left to right; assignment operators are evaluated right to left.
Task 05:
public class Precedence {
public static void main(String[] args) {
System.out.println( 3 + 3 * 2 );
System.out.println( 3 * 3 - 2 );
System.out.println( 3 * 3 / 2 );
System.out.println("--");
System.out.println( 1 * 1 + 1 * 1);
System.out.println( 1 + 1 / 1 - 1);
System.out.println( 3 * 3 / 2 + 2);
System.out.println("--");
}
}