a=4; // a takes the value 4 b=a+1; // b takes the value 5 d=c=a+b; // both d and c are 9
a=4; // a takes the value 4 b=-a; // b takes the value -4 but a is still 4.
a=4+5; // a takes the value 9 b=-a+4; // b takes the value -5
a=4; // a takes the value 4 a++; // a takes the value 5 ++a; // a takes the value 6 b=++a; // a and b take the value 7 (prefix operator) c=a++; // c takes the value 7 but a becomes 8 (postfix operator)
a=4-5; // a takes the value -1 b=-a-4; // b takes the value -3
a=10; // a takes the value 10 a--; // a takes the value 9 --a; // a takes the value 8 b=--a; // a and b take the value 7 (prefix operator) c=a--; // c takes the value 7 but a becomes 6 (postfix operator)
a=4*5; // a takes the value 20 b=-a*2; // b takes the value -40
int a=4/2; // a takes the value 2 int b= a/3; // b takes the value 0 double da=4.0/2.0; // da takes the value 2.0 double db=da/3.0; // db takes the value 0.6666666666666666 double dc=da/0.0; // dc takes the value Infinity double dd=0.0/0.0; // dd takes the value NaN
int a= 8 % 2; // a takes the value 0 since 8=4*2+0 int b= 7 % 3; // b takes the value 1 since 7=3*2+1 double da=8 % 2.0; // da takes 0.0 double db=7.0 /3.0; // db takes 1.0Where one or both of the operand is negative the % operator does not follow the normal mathematical convention. In these cases the result has the same sign as the first operand. For example:
int a= -8 % 2; // a takes the value 0 since -8=-4*2+0 int b= -7 % 3; // b takes the value 1 since -7=3*-2-1 int c= 7 %-3; // c takes the value 1 since 7=-3*-2+1 int d= -7 %-3; // d takes the value -1 since -7=-3*2-1This behavior is consistent with the c and c++ programming languages.
/*
This program demonstrates basic integer arithmetic operations variables in Java
*/
class BasicVariables2
{
public static void main(String[] args)
{
int iAnInteger=77;
int iASecondInteger=87;
System.out.println("the value stored in iAnInteger is");
System.out.println(iAnInteger);
System.out.println("the value stored in iASecondInteger is");
System.out.println(iASecondInteger);
iAnInteger++;
System.out.println("incrementing iAnInteger gives");
System.out.println(iAnInteger);
iAnInteger--;
iAnInteger--;
System.out.println("decrementing iAnInteger twice gives");
System.out.println(iAnInteger);
iAnInteger=5+iASecondInteger*4;
System.out.println("5+iASecondInteger*4 gives");
System.out.println(iAnInteger);
iAnInteger=5-iASecondInteger/4;
System.out.println("iAnInteger=5-iASecondInteger/4 gives");
System.out.println(iAnInteger);
iAnInteger=iASecondInteger % 2;
System.out.println("iASecondInteger % 2 gives");
System.out.println(iAnInteger);
}
}
$ $javac BasicVariables2.java $ java BasicVariables2 the value stored in iAnInteger is 77 the value stored in iASecondInteger is 87 incrementing iAnInteger gives 78 decrementing iAnInteger twice gives 76 5+iASecondInteger*4 gives 353 iAnInteger=5-iASecondInteger/4 gives -16 iASecondInteger % 2 gives 1 $ javac BasicVariables2.java $ java BasicVariables2 the value stored in iAnInteger is 77 the value stored in iASecondInteger is 87 incrementing iAnInteger gives 78 decrementing iAnInteger twice gives 76 5+iASecondInteger*4 gives 353 iAnInteger=5-iASecondInteger/4 gives -16 iASecondInteger % 2 gives 1 $
/**
This program demonstrates the modulus operator in Java
*/
class ModulusJava
{
public static void main(String[] args)
{
// integer modulus operator
int a=8%2;
int b=7%3;
int c=-7%3;
int d=7%-3;
int e=-7%-3;
int f=0%3;
int g=-0%3;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
//floating modulus operator
double da=8.0%2.0;
double db=7.0%3.0;
double dc=-7.0%3.0;
double dd=7.0%-3.0;
double de=-7.0%-3.0;
double df= 0.0%3.0;
double dg= 7.0%0.0;
double dh= 0.0%0.0;
System.out.println(da);
System.out.println(db);
System.out.println(dc);
System.out.println(dd);
System.out.println(de);
System.out.println(df);
System.out.println(dg);
System.out.println(dh);
}
}
$ javac ModulusJava.java $ java ModulusJava 0 1 -1 1 -1 0 0 0.0 1.0 -1.0 1.0 -1.0 0.0 NaN NaN $