412 lines
7.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// operators/AllOps.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Tests all operators on all primitive data types
// to show which ones are accepted by the Java compiler
2015-06-15 17:47:35 -07:00
public class AllOps {
// To accept the results of a Boolean test:
2015-06-15 17:47:35 -07:00
void f(boolean b) {}
void boolTest(boolean x, boolean y) {
// Arithmetic operators:
2015-12-18 11:28:19 -08:00
//- x = x * y;
//- x = x / y;
//- x = x % y;
//- x = x + y;
//- x = x - y;
//- x++;
//- x--;
//- x = +y;
//- x = -y;
2015-06-15 17:47:35 -07:00
// Relational and logical:
2015-12-18 11:28:19 -08:00
//- f(x > y);
//- f(x >= y);
//- f(x < y);
//- f(x <= y);
2015-06-15 17:47:35 -07:00
f(x == y);
f(x != y);
f(!y);
x = x && y;
x = x || y;
// Bitwise operators:
2015-12-18 11:28:19 -08:00
//- x = ~y;
2015-06-15 17:47:35 -07:00
x = x & y;
x = x | y;
x = x ^ y;
2015-12-18 11:28:19 -08:00
//- x = x << 1;
//- x = x >> 1;
//- x = x >>> 1;
2015-06-15 17:47:35 -07:00
// Compound assignment:
2015-12-18 11:28:19 -08:00
//- x += y;
//- x -= y;
//- x *= y;
//- x /= y;
//- x %= y;
//- x <<= 1;
//- x >>= 1;
//- x >>>= 1;
2015-06-15 17:47:35 -07:00
x &= y;
x ^= y;
x |= y;
// Casting:
2015-12-18 11:28:19 -08:00
//- char c = (char)x;
//- byte b = (byte)x;
//- short s = (short)x;
//- int i = (int)x;
//- long l = (long)x;
//- float f = (float)x;
//- double d = (double)x;
2015-06-15 17:47:35 -07:00
}
void charTest(char x, char y) {
// Arithmetic operators:
x = (char)(x * y);
x = (char)(x / y);
x = (char)(x % y);
x = (char)(x + y);
x = (char)(x - y);
x++;
x--;
2016-12-07 10:34:41 -08:00
x = (char) + y;
x = (char) - y;
2015-06-15 17:47:35 -07:00
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
x= (char)~y;
x = (char)(x & y);
x = (char)(x | y);
x = (char)(x ^ y);
x = (char)(x << 1);
x = (char)(x >> 1);
x = (char)(x >>> 1);
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void byteTest(byte x, byte y) {
// Arithmetic operators:
x = (byte)(x* y);
x = (byte)(x / y);
x = (byte)(x % y);
x = (byte)(x + y);
x = (byte)(x - y);
x++;
x--;
2016-12-07 10:34:41 -08:00
x = (byte) + y;
x = (byte) - y;
2015-06-15 17:47:35 -07:00
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
x = (byte)~y;
x = (byte)(x & y);
x = (byte)(x | y);
x = (byte)(x ^ y);
x = (byte)(x << 1);
x = (byte)(x >> 1);
x = (byte)(x >>> 1);
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
char c = (char)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void shortTest(short x, short y) {
// Arithmetic operators:
x = (short)(x * y);
x = (short)(x / y);
x = (short)(x % y);
x = (short)(x + y);
x = (short)(x - y);
x++;
x--;
2016-12-07 10:34:41 -08:00
x = (short) + y;
x = (short) - y;
2015-06-15 17:47:35 -07:00
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
2016-12-07 10:34:41 -08:00
x = (short) ~ y;
2015-06-15 17:47:35 -07:00
x = (short)(x & y);
x = (short)(x | y);
x = (short)(x ^ y);
x = (short)(x << 1);
x = (short)(x >> 1);
x = (short)(x >>> 1);
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
char c = (char)x;
byte b = (byte)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void intTest(int x, int y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void longTest(long x, long y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
float f = (float)x;
double d = (double)x;
}
void floatTest(float x, float y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
2015-12-18 11:28:19 -08:00
//- x = ~y;
//- x = x & y;
//- x = x | y;
//- x = x ^ y;
//- x = x << 1;
//- x = x >> 1;
//- x = x >>> 1;
2015-06-15 17:47:35 -07:00
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
2015-12-18 11:28:19 -08:00
//- x <<= 1;
//- x >>= 1;
//- x >>>= 1;
//- x &= y;
//- x ^= y;
//- x |= y;
2015-06-15 17:47:35 -07:00
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
double d = (double)x;
}
void doubleTest(double x, double y) {
// Arithmetic operators:
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical:
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
2015-12-18 11:28:19 -08:00
//- f(!x);
//- f(x && y);
//- f(x || y);
2015-06-15 17:47:35 -07:00
// Bitwise operators:
2015-12-18 11:28:19 -08:00
//- x = ~y;
//- x = x & y;
//- x = x | y;
//- x = x ^ y;
//- x = x << 1;
//- x = x >> 1;
//- x = x >>> 1;
2015-06-15 17:47:35 -07:00
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
2015-12-18 11:28:19 -08:00
//- x <<= 1;
//- x >>= 1;
//- x >>>= 1;
//- x &= y;
//- x ^= y;
//- x |= y;
2015-06-15 17:47:35 -07:00
// Casting:
2015-12-18 11:28:19 -08:00
//- boolean bl = (boolean)x;
2015-06-15 17:47:35 -07:00
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
}
2015-09-07 11:44:36 -06:00
}