© 2001 by ashby m. woolf revision 3 using java operators the basic toolkit pliers, screwdriver,...
TRANSCRIPT
![Page 1: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/1.jpg)
© 2001 by Ashby M. Woolf Revision 3
Using Java Operators
The Basic Toolkit
Pliers, Screwdriver, Hammer and Drill
![Page 2: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/2.jpg)
© 2001 by Ashby M. Woolf Revision 3
Assignment Operator (=)lvalue = rvalue;
• 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.
w = 10;
x = w;
z = (x - 2)/(2 + 2);
![Page 3: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/3.jpg)
© 2001 by Ashby M. Woolf Revision 3
Mathematical Operators
• Addition +• Subtraction -• Multiplication *• Division /• Modulus %
![Page 4: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/4.jpg)
© 2001 by Ashby M. Woolf Revision 3
Simple Arithmeticpublic 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 >
![Page 5: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/5.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shorthand Operators+=, -=, *=, /=, %=
Common Shorthand
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
a = a % b; a %= b;
![Page 6: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/6.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shorthand Operatorspublic 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 = 6q = -3r = 15s = 0t = 0>
![Page 7: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/7.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shorthand Increment and Decrement ++ and --
Common Shorthand
a = a + 1; a++; or ++a;
a = a - 1; a--; or --a;
![Page 8: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/8.jpg)
© 2001 by Ashby M. Woolf Revision 3
Increment and Decrement
> java examplep = 6q = 6j = 7r = 6s = 6>
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);
}}
![Page 9: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/9.jpg)
© 2001 by Ashby M. Woolf Revision 3
Arithmetic
Operators
A Change of Topic
Casting
long64 bit
l
int32 bit
i
![Page 10: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/10.jpg)
© 2001 by Ashby M. Woolf Revision 3
long64 bit
int32 bit
liint i;
i = (int)l;
l = (long)i;
Moving Between Buckets"Casting"
long l;
l = i;
long64 bit
int32 bit
li
Widening
Narrowing
![Page 11: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/11.jpg)
© 2001 by Ashby M. Woolf Revision 3
Declaration Size and Type Number Range
byte b; // 8 bit integer -2-7 to +2+7-1
short s; // 16 bit integer -2-15 to +2+15-1
char c; // 16 bit Unicode 0 to +2+16
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 IEEE754 Standard
double d;// 64 bit floating point IEEE754 Standard
The Primitive Numbers
![Page 12: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/12.jpg)
© 2001 by Ashby M. Woolf Revision 3
Casting the Primitives
b = (byte)s;b = (byte)c;b = (byte)i;b = (byte)l;b = (byte)f;b = (byte)d;
i = (int)b;i = (int)s;i = (int)c;i = (int)l;i = (int)f;i = (int)d;
l = (long)b;l = (long)s;l = (long)c;l = (long)i;l = (long)f;l = (long)d;
s = (short)b;s = (short)c;s = (short)i;s = (short)l;s = (short)f;s = (short)d;
2's
Co
mp
lem
ent
c = (char)b;c = (char)s;c = (char)i;c = (char)l;c = (char)f;c = (char)d;P
osi
tive
On
ly
f = (float)b;f = (float)s;f = (float)c;f = (float)i;f = (float)l;f = (float)d;
d = (double)b;d = (double)s;d = (double)c;d = (double)i;d = (double)l;d = (double)f;F
loat
ing
Po
int
b = (byte)s;b = (byte)c;b = (byte)i;b = (byte)l;b = (byte)f;b = (byte)d;
i = b;i = s;i = c;i = (int)l;i = (int)f;i = (int)d;
l = b;l = s;l = c;l = i;l = (long)f;l = (long)d;
s = b;s = (short)c;s = (short)i;s = (short)l;s = (short)f;s = (short)d;2'
s C
om
ple
men
t
c = (char)b;c = (char)s;c = (char)i;c = (char)l;c = (char)f;c = (char)d;P
osi
tive
On
ly
f = b;f = s;f = c;f = i;f = l;f = (float)d;
d = b;d = s;d = c;d = i;d = l;d = f;F
loat
ing
Po
int
![Page 13: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/13.jpg)
© 2001 by Ashby M. Woolf Revision 3
Acceptable Implicit Casts
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Illegal b = l;l = f;c = s;
OKl = b;i = c;f = l
![Page 14: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/14.jpg)
© 2001 by Ashby M. Woolf Revision 3
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Automatic Promotion with Arithmetic
Illegal Castss = s + b;s = s + s;
OKs = (short)(s + b);s = (short)(s + s);
Arithmetic is never donein 8 or 16 bit containers.
![Page 15: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/15.jpg)
© 2001 by Ashby M. Woolf Revision 3
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Arithmetic Promotion with Mixed Primitives
Illegal Castsi = i + l;f = f + d;l = l + f;
OKi = (int)(i + l);d = (double)(f + d);l = (long)(l + f);
Arithmetic is donein the "widest" type.
![Page 16: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/16.jpg)
© 2001 by Ashby M. Woolf Revision 3
Implicit Casts in Method Calls
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Illegal i = st.indexOf(f);
OKi = st.indexOf(c);i = st.indexOf(b);
For: String st; and,public int indexOf(int ch);
![Page 17: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/17.jpg)
© 2001 by Ashby M. Woolf Revision 3
Casting
A Change of Topic
The Logicaland
Relational Operators
![Page 18: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/18.jpg)
© 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
![Page 19: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/19.jpg)
© 2001 by Ashby M. Woolf Revision 3
Relational Operator Examplespublic 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));System.out.println("p > r " + (p > r));System.out.println("p == q " + (p == q));System.out.println("p != q " + (p != q));
System.out.println("i == j " + (i == j));System.out.println("i != j " + (i != j));
}} > java Example
p < r true p > r false p == q true p != q false i == j false i != j true >
![Page 20: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/20.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical Operators (boolean) && || !
• Logical AND &&
• Logical OR ||
• Logical NOT !
![Page 21: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/21.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical (&&) Operator Examples
public class Example {public static void main(String[] args) {
boolean t = true;boolean f = false;
System.out.println("f && f " + (f && f));System.out.println("f && t " + (f && t));System.out.println("t && f " + (t && f));System.out.println("t && t " + (t && t));
}}
> java Example f && f false f && t false t && f false t && t true >
![Page 22: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/22.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical (||) Operator Examples
public class Example {public static void main(String[] args) {
boolean t = true;boolean f = false;
System.out.println("f || f " + (f || f));System.out.println("f || t " + (f || t));System.out.println("t || f " + (t || f));System.out.println("t || t " + (t || t));
}}
> java Example f || f false f || t true t || f true t || t true >
![Page 23: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/23.jpg)
© 2001 by Ashby M. Woolf Revision 3
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 >
![Page 24: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/24.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical Operator ExamplesShort 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 > java Example b, j, k true 1, 1 b, j, k false 1, 0 >
![Page 25: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/25.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical Operator ExamplesShort 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 > java Example b, j, k true 1, 0 b, j, k true 1, 1 >
![Page 26: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/26.jpg)
© 2001 by Ashby M. Woolf Revision 3
The Logical andRelational Operators
A Change of Topic
Manipulatingthe Bits 10010111
![Page 27: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/27.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical Operators (Bit Level) & | ^ ~
• AND &• OR |• XOR ^• NOT ~
![Page 28: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/28.jpg)
© 2001 by Ashby M. Woolf Revision 3
Twos Complement NumbersBase 10 A byte of binary
+127 01111111
+4 00000100
+3 00000011
+2 00000010
+1 00000001
+0 00000000
-1 11111111
-2 11111110
-3 11111101
-4 11111100
-128 10000000
![Page 29: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/29.jpg)
© 2001 by Ashby M. Woolf Revision 3
Adding Twos Complements
Base 10 Binary
+3 00000011
-2 11111110
+1 00000001
Base 10 Binary
+2 00000010
-3 11111101
-1 11111111
![Page 30: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/30.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical Operators (Bit Level) & | ^ ~
int a = 10; // 00001010 = 10int b = 12; // 00001100 = 12
a 00000000000000000000000000001010 10b 00000000000000000000000000001100 12a & b 00000000000000000000000000001000 8
a 00000000000000000000000000001010 10b 00000000000000000000000000001100 12a | b 00000000000000000000000000001110 14
a 00000000000000000000000000001010 10b 00000000000000000000000000001100 12a ^ b 00000000000000000000000000000110 6
a 00000000000000000000000000001010 10~a 11111111111111111111111111110101 -11
&AND
|OR
^XOR
~NOT
![Page 31: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/31.jpg)
© 2001 by Ashby M. Woolf Revision 3
Logical (bit) Operator Examplespublic class Example {
public static void main(String[] args) {int a = 10; // 00001010 = 10int b = 12; // 00001100 = 12int and, or, xor, na;and = a & b; // 00001000 = 8or = a | b; // 00001110 = 14xor = a ^ b; // 00000110 = 6na = ~a; // 11110101 = -11System.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 >
![Page 32: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/32.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shift Operators (Bit Level) << >> >>>
• Shift Left << Fill with Zeros
• Shift Right >> Based on Sign
• Shift Right >>> Fill with Zeros
![Page 33: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/33.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shift Operators << >>
int a = 3; // ...00000011 = 3int b = -4; // ...11111100 = -4
a 00000000000000000000000000000011 3a << 2 00000000000000000000000000001100 12
b 11111111111111111111111111111100 -4b << 2 11111111111111111111111111110000 -16
<<Left
>>Right
a 00000000000000000000000000000011 3a >> 2 00000000000000000000000000000000 0
b 11111111111111111111111111111100 -4b >> 2 11111111111111111111111111111111 -1
![Page 34: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/34.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shift Operator >>>
int a = 3; // ...00000011 = 3int b = -4; // ...11111100 = -4
>>>Right 0
a 00000000000000000000000000000011 3a >>> 2 00000000000000000000000000000000 0
b 11111111111111111111111111111100 -4b >>> 2 00111111111111111111111111111111 +big
![Page 35: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/35.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shift Operator Examplespublic class Example {
public static void main(String[] args) {int a = 3; // ...00000011 = 3int 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 >
![Page 36: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/36.jpg)
© 2001 by Ashby M. Woolf Revision 3
Shift Operator >>> and Automatic Arithmetic Promotion
byte a = 3; // 00000011 = 3byte b = -4; // 11111100 = -4byte c;c = (byte) a >>> 2c = (byte) b >>> 2
>>>RightFill 0
a 00000011 3a >>> 2 00000000000000000000000000000000 0c = (byte) 00000000 0
b 11111100 -4b >>> 2 00111111111111111111111111111111 1073741823c = (byte) Much to big for byte 11111111 -1
![Page 37: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/37.jpg)
© 2001 by Ashby M. Woolf Revision 3
Which Operators Operate On What
Operators
&& || !
Unary+ - ++ --
+ - * / %
> < >= <=
== !=
<< >> >>>& | ^ ~
=op= etc.
floatdouble
Floating Point
char byteshortintlongAutomaticPromotion
Except ++ - -
AutomaticPromotion
Except ++ - -
AutomaticPromotion
Except ++ - -
AutomaticPromotion
Except ++ - -
AutomaticPromotion
Except ++ - -
AutomaticPromotion
Except ++ - -
AutomaticPromotion
AutomaticPromotion
AutomaticPromotion
Integral
boolean
Logical AnyObject
+ withStringOnly
ReferenceOnly
Not Content
![Page 38: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/38.jpg)
© 2001 by Ashby M. Woolf Revision 3
Assignment Operator (=) and Classes
Date x = new Date();
Date y = new Date();
x = y;
![Page 39: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/39.jpg)
© 2001 by Ashby M. Woolf Revision 3
Assignment Operator (=) and Classes
Date x = new Date();
Date y = new Date();
x = y;
![Page 40: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/40.jpg)
© 2001 by Ashby M. Woolf Revision 3
Manipulatingthe Bits
A Change of Topic
Some Oddsand Ends
![Page 41: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/41.jpg)
© 2001 by Ashby M. Woolf Revision 3
Ternary Operator ? :
If true this expression is evaluated and becomes the value entire expression.
Any expression that evaluatesto a boolean value.
If false this expression is evaluated and becomes the value entire expression.
boolean_expression ? expression_1 : expression_2
![Page 42: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/42.jpg)
© 2001 by Ashby M. Woolf 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 "+(t ? true : false ));System.out.println("t?1:2 "+(t ? 1 : 2 ));System.out.println("f?true:false "+(f ? true : false ));System.out.println("f?1:2 "+(f ? 1 : 2 ));
}}
> java Example t?true:false true t?1:2 1 f?true:false false f?1:2 2 >
![Page 43: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/43.jpg)
© 2001 by Ashby M. Woolf Revision 3
String (+) OperatorString Concatenation
"Now is " + "the time."
"Now is the time."
![Page 44: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/44.jpg)
© 2001 by Ashby M. Woolf Revision 3
String (+) OperatorAutomatic Conversion to a String
If either expression_1If either expression_1 or expression_2 evaluatesto a string the other will be converted to a string if needed. The result will be their concatenation.
expression_1 + expression_2
![Page 45: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/45.jpg)
© 2001 by Ashby M. Woolf Revision 3
String (+) OperatorAutomatic Conversion with Primitives
"The number is " + 4
"The number is " + "4"
"The number is 4"
![Page 46: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/46.jpg)
© 2001 by Ashby M. Woolf Revision 3
String (+) OperatorAutomatic Conversion with Objects
"Today is " + new Date()
"Today is " + "Wed 27 22:12;26 CST 2000"
"Today is Wed 27 22:12;26 CST 2000"
"Today is " + new Date().toString()
![Page 47: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/47.jpg)
© 2001 by Ashby M. Woolf Revision 3
Operator Precedence
+ - ++ -- ! ~ ()
* / %
+ -
<< >> >>>
> < >= <= instanceof
== !=
& | ^
&& ||
?:
= (and += etc.)
Unary
Arithmetic
Shift
Comparison
Logical Bit
Boolean
Ternary
Assignment
![Page 48: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/48.jpg)
© 2001 by Ashby M. Woolf Revision 3
Passing Classes to Methodsclass Letter { char c;}
public class PassObject { 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 }}
![Page 49: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/49.jpg)
© 2001 by Ashby M. Woolf Revision 3
Passing Primitives to Methodsclass Letter { char c;}
public class PassPrimitive { 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 }}
![Page 50: © 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill](https://reader036.vdocuments.mx/reader036/viewer/2022062516/56649e235503460f94b101e3/html5/thumbnails/50.jpg)
© 2001 by Ashby M. Woolf Revision 3
End of Content