Using Java Operators The Basic Toolkit Pliers Screwdriver

Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill © 2001 by Ashby M. Woolf Revision 3

Assignment Operator (=) lvalue = rvalue; w = 10; x = w; z = (x - 2)/(2 + 2); • Take the value of the rvalue and store it in the lvalue. • The rvalue is any constant, variable or expression. • The lvalue is named variable. © 2001 by Ashby M. Woolf Revision 3

Mathematical Operators • • • © 2001 by Ashby M. Woolf Addition Subtraction Multiplication Division Modulus + * / % Revision 3
![Simple Arithmetic public class Example { public static void main(String[] args) { int j, Simple Arithmetic public class Example { public static void main(String[] args) { int j,](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-4.jpg)
Simple Arithmetic public class Example { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System. out. println("p = " + p); System. out. println("q = " + q); System. out. println("r = " + r); System. out. println("s = " + s); System. out. println("t = " + t); } > java Example p = 7 } q = 3 r = 10 s = 2 t = 1 > © 2001 by Ashby M. Woolf Revision 3

Shorthand Operators +=, -=, *=, /=, %= Common a = a + a = a * a = a / a = a % © 2001 by Ashby M. Woolf b; b; b; Shorthand a += a -= a *= a /= a %= b; b; b; Revision 3
![Shorthand Operators public class Example { public static void main(String[] args) { int j, Shorthand Operators public class Example { public static void main(String[] args) { int j,](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-6.jpg)
Shorthand Operators public class Example { public static void main(String[] args) { int j, p, q, r, s, t; j = 5; p = 1; q = 2; r = 3; s = 4; t = 5; p += j; q -= j; r *= j; s /= j; t %= j; System. out. println("p = " + p); System. out. println("q = " + q); System. out. println("r = " + r); System. out. println("s = " + s); System. out. println("t = " + t); } > java Example p = 6 } q = -3 r = 15 s = 0 t = 0 > © 2001 by Ashby M. Woolf Revision 3

Shorthand Increment and Decrement ++ and -Common a = a + 1; ++a; a = a - 1; --a; © 2001 by Ashby M. Woolf Shorthand a++; or a--; or Revision 3
![Increment and Decrement public class Example { public static void main(String[] args) { int Increment and Decrement public class Example { public static void main(String[] args) { int](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-8.jpg)
Increment and Decrement public class Example { public static void main(String[] args) { int j, p, q, r, s; j = 5; p = ++j; // j = j + 1; p = j; System. out. println("p = " + p); q = j++; // q = j; j = j + 1; System. out. println("q = " + q); System. out. println("j = " + j); r = --j; // j = j -1; r = j; System. out. println("r = " + r); s = j--; // s = j; j = j - 1; System. out. println("s = " + s); } } > java example p = 6 q = 6 j = 7 r = 6 s = 6 > © 2001 by Ashby M. Woolf Revision 3

A Change of Topic Arithmetic Operators l i int 32 bit long 64 bit Casting © 2001 by Ashby M. Woolf Revision 3

Moving Between Buckets "Casting" int i; i int 32 bit © 2001 by Ashby M. Woolf i = (int)l; Narrowing l = i; (long)i; Widening l long l; long 64 bit Revision 3

The Primitive Numbers Declaration Size and Type Number Range byte b; // -2 -7 to +2+7 -1 short s; // 16 bit integer char c; // 16 bit Unicode int i; // 32 bit integer -2 -31 to +2+31 -1 long l; // 64 bit integer -2 -63 to +2+63 -1 float f; // 32 bit floating point IEEE 754 Standard double d; // 64 bit floating point IEEE 754 Standard © 2001 by Ashby M. Woolf 8 bit integer -2 -15 to +2+15 -1 0 to +2+16 Revision 3

Casting the Primitives b b b = = = (byte)s; (byte)c; (byte)i; (byte)l; (byte)f; (byte)d; Floating Point 2's Complement Positive Only c c c © 2001 by Ashby M. Woolf f f f = = = s s s = = = (char)b; (char)s; (char)i; (char)l; (char)f; (char)d; b; (short)c; (short)i; (short)l; (short)f; (short)d; (float)b; b; (float)s; s; (float)c; c; (float)i; i; (float)l; l; (float)d; i i i = = = d d d b; (int)s; s; (int)c; c; (int)l; (int)f; (int)d; = = = l l l = = = b; (long)s; s; (long)c; c; (long)i; i; (long)f; (long)d; (double)b; b; (double)s; s; (double)c; c; (double)i; i; (double)l; l; (double)f; f; Revision 3

Acceptable Implicit Casts char 16 bit byte 8 bit short 16 bit Illegal b = l; l = f; c = s; © 2001 by Ashby M. Woolf int 32 bit long 64 bit float 32 bit double 64 bit OK l = b; i = c; f = l Revision 3

Automatic Promotion with Arithmetic is never done in 8 or 16 bit containers. char 16 bit byte 8 bit short 16 bit int 32 bit Illegal Casts s = s + b; s = s + s; © 2001 by Ashby M. Woolf long 64 bit float 32 bit double 64 bit OK s = (short)(s + b); s = (short)(s + s); Revision 3

Arithmetic Promotion with Mixed Primitives Arithmetic is done in the "widest" type. char 16 bit byte 8 bit short 16 bit int 32 bit Illegal Casts i = i + l; f = f + d; l = l + f; © 2001 by Ashby M. Woolf long 64 bit float 32 bit double 64 bit OK i = (int)(i + l); d = (double)(f + d); l = (long)(l + f); Revision 3

Implicit Casts in Method Calls char 16 bit byte 8 bit short 16 bit int 32 bit long 64 bit float 32 bit double 64 bit For: String st; and, public int index. Of(int ch); Illegal i = st. index. Of(f); © 2001 by Ashby M. Woolf OK i = st. index. Of(c); i = st. index. Of(b); Revision 3

A Change of Topic Casting The Logical and Relational Operators © 2001 by Ashby M. Woolf Revision 3

Relational Operators > < >= <= Primitives • Greater Than • Less Than • Greater Than or Equal • Less Than or Equal == != > < >= <= Primitives or Object References • Equal (Equivalent) == • Not Equal != The Result is Always true or false © 2001 by Ashby M. Woolf Revision 3
![Relational Operator Examples public class Example { public static void main(String[] args) { int Relational Operator Examples public class Example { public static void main(String[] args) { int](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-19.jpg)
Relational Operator Examples public class Example { public static void main(String[] args) { int p =2; int q = 2; int r = 3; Integer i = new Integer(10); Integer j = new Integer(10); System. out. println("p < r " + (p < r)); > r " + (p > r)); == q " + (p == q)); != q " + (p != q)); System. out. println("i == j " + (i == j)); System. out. println("i != j " + (i != j)); } } © 2001 by Ashby M. Woolf > p p i i > java Example < r true > r false == q true != q false == j false != j true Revision 3

Logical Operators (boolean) && © 2001 by Ashby M. Woolf || ! • Logical AND && • Logical OR || • Logical NOT ! Revision 3
![Logical (&&) Operator Examples public class Example { public static void main(String[] args) { Logical (&&) Operator Examples public class Example { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-21.jpg)
Logical (&&) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System. out. println("f System. out. println("t && && f t " " + + (f (f (t (t && && f)); t)); } } © 2001 by Ashby M. Woolf > f f t t > java && f && t Example false true Revision 3
![Logical (||) Operator Examples public class Example { public static void main(String[] args) { Logical (||) Operator Examples public class Example { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-22.jpg)
Logical (||) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System. out. println("f System. out. println("t || || f t " " + + (f (f (t (t || || f)); t)); } } © 2001 by Ashby M. Woolf > f f t t > java || f || t Example false true Revision 3
![Logical (!) Operator Examples public class Example { public static void main(String[] args) { Logical (!) Operator Examples public class Example { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-23.jpg)
Logical (!) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System. out. println("!f " + !f); System. out. println("!t " + !t); } } > java Example !f true !t false > © 2001 by Ashby M. Woolf Revision 3

Logical Operator Examples Short Circuiting with && public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) && ( j == ++k ); System. out. println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) && ( j == ++k ); System. out. println("b, j, k " + b + ", " + j + ", " + k); } } > java Example b, j, k true 1, 1 b, j, k false 1, 0 > © 2001 by Ashby M. Woolf Revision 3

Logical Operator Examples Short Circuiting with || public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) || ( j == ++k ); System. out. println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) || ( j == ++k ); System. out. println("b, j, k " + b + ", " + j + ", " + k); } } > java Example b, j, k true 1, 0 b, j, k true 1, 1 > © 2001 by Ashby M. Woolf Revision 3

A Change of Topic The Logical and Relational Operators Manipulating the Bits 10010111 © 2001 by Ashby M. Woolf Revision 3

Logical Operators (Bit Level) & • • © 2001 by Ashby M. Woolf AND OR XOR NOT | ^ ~ & | ^ ~ Revision 3

Twos Complement Numbers © 2001 by Ashby M. Woolf Base 10 +127 A byte of binary 01111111 +4 +3 +2 +1 +0 -1 -2 -3 -4 00000100 00000011 00000010 00000001 0000 11111110 11111101 11111100 -128 10000000 Revision 3

Adding Twos Complements Base 10 +3 -2 +1 © 2001 by Ashby M. Woolf Binary 00000011 11111110 00000001 Base 10 +2 -3 -1 Binary 00000010 11111101 1111 Revision 3

Logical Operators (Bit Level) & | ^ ~ int a = 10; // 00001010 = 10 int b = 12; // 00001100 = 12 & a b a & b 000000000000001010 000000000000001100 000000000000001000 10 12 8 | a b a | b 000000000000001010 000000000000001100 000000000000001110 10 12 14 ^ a b a ^ b 000000000000001010 000000000000001100 000000000000000110 10 12 6 a ~a 000000000000001010 111111111111110101 10 -11 AND OR XOR ~ NOT © 2001 by Ashby M. Woolf Revision 3
![Logical (bit) Operator Examples public class Example { public static void main(String[] args) { Logical (bit) Operator Examples public class Example { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-31.jpg)
Logical (bit) Operator Examples public class Example { public static void main(String[] args) { int a = 10; // 00001010 = 10 int b = 12; // 00001100 = 12 int and, or, xor, na; and = a & b; // 00001000 = 8 or = a | b; // 00001110 = 14 xor = a ^ b; // 00000110 = 6 na = ~a; // 11110101 = -11 System. out. println("and " + and); System. out. println("or " + or); System. out. println("xor " + xor); System. out. println("na " + na); } } > java Example and 8 or 14 xor 6 na -11 > © 2001 by Ashby M. Woolf Revision 3

Shift Operators (Bit Level) << © 2001 by Ashby M. Woolf >> >>> • Shift Left << Fill with Zeros • Shift Right >> Based on Sign • Shift Right >>> Fill with Zeros Revision 3

Shift Operators << >> int a = 3; //. . . 00000011 = 3 int b = -4; //. . . 11111100 = -4 << Left >> Right a a << 2 0000000000000001100 3 12 b b << 2 1111111111111110000 -4 -16 a a >> 2 00000000000000011 0000000000000000 3 0 b b >> 2 11111111111111100 1111111111111111 -4 -1 © 2001 by Ashby M. Woolf Revision 3

Shift Operator >>> int a = 3; //. . . 00000011 = 3 int b = -4; //. . . 11111100 = -4 >>> Right 0 a 00000000000000011 a >>> 2 0000000000000000 3 0 b 11111111111111100 b >>> 2 00111111111111111 -4 +big © 2001 by Ashby M. Woolf Revision 3
![Shift Operator Examples public class Example { public static void main(String[] args) { int Shift Operator Examples public class Example { public static void main(String[] args) { int](http://slidetodoc.com/presentation_image_h2/5fc6ee8592018860f83c6bec3afe7f4a/image-35.jpg)
Shift Operator Examples public class Example { public static void main(String[] args) { int a = 3; //. . . 00000011 = 3 int b = -4; //. . . 11111100 = -4 System. out. println("a<<2 = " + (a<<2)); System. out. println("b<<2 = " + (b<<2)); System. out. println("a>>2 = " + (a>>2)); System. out. println("b>>2 = " + (b>>2)); System. out. println("a>>>2 = " + (a>>>2)); System. out. println("b>>>2 = " + (b>>>2)); } } > java Example a<<2 = 12 b<<2 = -16 a>>2 = 0 b>>2 = -1 a>>>2 = 0 b>>>2 = 1073741823 > © 2001 by Ashby M. Woolf Revision 3

Shift Operator >>> and Automatic Arithmetic Promotion byte a = 3; byte b = -4; byte c; c = (byte) a c = (byte) b >>> Right Fill 0 // 00000011 = 3 // 11111100 = -4 >>> 2 a 00000011 a >>> 2 0000000000000000 c = (byte) 0000 3 0 0 b 11111100 -4 b >>> 2 00111111111111111 1073741823 c = (byte) Much to big for byte 1111 -1 © 2001 by Ashby M. Woolf Revision 3

Which Operators Operate On What Operators Unary + - ++ -+ - * / % Floating Point double float long int Integral short char byte Logical Any boolean Object Automatic Automatic Promotion Promotion Except ++ - - Except ++ - - + with String Only > < >= <= Reference Only Not Content == != = op= etc. << >> >>> & | ^ ~ Automatic Promotion && || ! © 2001 by Ashby M. Woolf Revision 3

Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y; © 2001 by Ashby M. Woolf Revision 3

Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y; © 2001 by Ashby M. Woolf Revision 3

A Change of Topic Manipulating the Bits Some Odds and Ends © 2001 by Ashby M. Woolf Revision 3

Ternary Operator ? : Any expression that evaluates to a boolean value. boolean_expression ? expression_1 : expression_2 If true this expression is evaluated and becomes the value entire expression. © 2001 by Ashby M. Woolf If false this expression is evaluated and becomes the value entire expression. Revision 3

Ternary ( ? : ) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System. out. println("t? true: false System. out. println("t? 1: 2 "+(t ? System. out. println("f? true: false System. out. println("f? 1: 2 "+(f ? "+(t ? true : false )); 1 : 2 )); "+(f ? true : false )); 1 : 2 )); } } > java Example t? true: false true t? 1: 2 1 f? true: false f? 1: 2 2 > © 2001 by Ashby M. Woolf Revision 3

String (+) Operator String Concatenation "Now is " + "the time. " "Now is the time. " © 2001 by Ashby M. Woolf Revision 3

String (+) Operator Automatic Conversion to a String expression_1 + expression_2 If either expression_1 or expression_2 evaluates to a string the other will be converted to a string if needed. The result will be their concatenation. © 2001 by Ashby M. Woolf Revision 3

String (+) Operator Automatic Conversion with Primitives "The number is " + 4 "The number is " + "4" "The number is 4" © 2001 by Ashby M. Woolf Revision 3

String (+) Operator Automatic Conversion with Objects "Today is " + new Date(). to. String() "Today is " + "Wed 27 22: 12; 26 CST 2000" "Today is Wed 27 22: 12; 26 CST 2000" © 2001 by Ashby M. Woolf Revision 3

Operator Precedence Unary Arithmetic Shift Comparison Logical Bit Boolean Ternary Assignment © 2001 by Ashby M. Woolf + - ++ -- ! ~ () * / % + << >> > < >= <= instanceof == != & | ^ && || ? : = (and += etc. ) Revision 3

Passing Classes to Methods class Letter { char c; } public class Pass. Object { static void f(Letter y) { y. c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x. c = 'a'; System. out. println("1: x. c: " + x. c); // Prints 1: x. c: a f(x); System. out. println("2: x. c: " + x. c); // Prints 2: x. c: z } } © 2001 by Ashby M. Woolf Revision 3

Passing Primitives to Methods class Letter { char c; } public class Pass. Primitive { static void f(char y) { y = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x. c = 'a'; System. out. println("1: x. c: " + x. c); // Prints 1: x. c: a f(x. c); System. out. println("2: x. c: " + x. c); // Prints 2: x. c: a } } © 2001 by Ashby M. Woolf Revision 3

End of Content © 2001 by Ashby M. Woolf Revision 3
- Slides: 50