Operator Code Display                Peter Komisar


class OpShow{
public static void main(String[] args){

// Unary + and - *******************************************************
  int a=+8;
  a=-a;
  System.out.println("\n" + "The int a was +8 and now is " + a + "\n" );

// Bitwise Inversion Operator *******************************************
  int b= 5;
  b = ~b;
  System.out.println("Inverting the binary form of b results in a value of  "
                      + b + "\n" );

// Boolean Complement Operator ! ****************************************
 
  boolean c=false;
  c = !c;
  System.out.println( "The boolean c, which was false is now " + c + "\n" );

// The cast operator **************************************************

  double d = 1.412;
  int i = (int) d;
  System.out.println("The double " + d + " becomes the int value "
                        + i +"\n");

// The multiplication and division operators ******************************

int bigOne=1000000000;        // one billion
int lever = 3;
int tooBig = bigOne * lever;   //overflow

int e = 9;
int f = 5;
int g = e / f;       // imprecision

System.out.println("Overflow caused tooBig to have a value " + g
                    + "  instead of 3 billion" + "\n");
System.out.println("The result of 9/5 stored in g is " + g +
                      " and suffers from imprecision" + "\n");

// The modulo operator % *****************************************************

int h = -7 % 2;     // h is assigned the value -1
System.out.println(" -7 modulo 2 is the remainder " + h + "\n");

// The Addition and Subtraction Operators + and - ****************************

int j = 1;
int k = 2;
int l = j + k;
System.out.println ("One plus two is " + l + "." +
                    "The numeric literal " + (1+"") +
                    ", here is promoted to String type" + "\n");

// The Increment and Decrement Operators *************************************

int startPoint = 7;
System.out.println("Post Increment: \n startPoint is at first " + startPoint);
int getFromPost = startPoint++;

System.out.println(" getFromPost is " + getFromPost +
                   ". startPoint is now" + startPoint);

int startPoint2 = 11;
System.out.println("Post Increment: \n startPoint2 is at first " + startPoint2);
int getFromPre = ++startPoint2;

System.out.println(" getFromPre is " + getFromPre +
                   ". startPoint2 is now " + startPoint2 + "\n");

// Signed right shift operator ***********************************************

  int v = 8 >> 2;
  System.out.println("8 right shifted two places yields " + v );
  v = -8 >> 2;
  System.out.println("8 right shifted two places yields " + v );
  v = -8 >>> 1;
  System.out.println
  ("-8 unsigned right shifted one places yields a big positive number,"
   + v +"\n" );

// The instanceof Operator ****************************************************

  String s ="X";
  if ( s instanceof Object )
     System.out.println
     ("The boolean must evaluate to true as all classes descend from Object \n");

// The ordinal comparison operator

  int px = 12;
  double dx= 244;
  if (px < dx )
    System.out.println(" True, px is less than dx" +"\n");
  }
}