public class operators
{
public static void main(String[] args)
{
int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;
// precedence rules for arithmetic operators.
// (* = / = %) > (+ = -)
// prints a+(b/d)
System.out.println("a+b/d = "+(a + b / d));
// if same precendence then associative
// rules are followed.
// e/f -> b*d -> a+(b*d) -> a+(b*d)-(e/f)
System.out.println("a+b*d-e/f = "+(a + b * d - e / f));
}
}
输出:
a + b / d = 20 a + b * de / f = 219
public class operators
{
public static void main(String[] args)
{
int a = 20, b = 10, c = 0;
// a=b+++c is compiled as
// b++ +c
// a=b+c then b=b+1
a = b+++c;
System.out.println("Value of a(b+c),b(b+1),c = " + a + "," + b + "," + c);
// a=b+++++c is compiled as
// b++ ++ +c
// which gives error.
// a=b+++++c;
// System.out.println(b+++++c);
}
}
输出:
Value of a(b+c),b(b+1),c = 10,11,0
public class operators
{
public static void main(String[] args)
{
int x = 5, y = 8;
// concatenates x and y
// as first x is added to "concatenation (x+y) = "
// producing "concatenation (x+y) = 5" and then 8 is
// further concatenated.
System.out.println("Concatenation (x+y)= " + x + y);
// addition of x and y
System.out.println("Addition (x+y) = " + (x + y));
}
}
输出:
Concatenation (x+y)= 58 Addition (x+y) = 13