1 //thinging in JAVA P1232 3 Packagejava_test;4 5 //Tests all the operators in all the primitive data types:6 //To show which ones accepted by the Java compiler.7 8 //This file would compile without error because9 //The lines that fail is commented out with a//!Ten Public classAllops { One //To accept the results of a Boolean test: A voidFBooleanb) { - } - the voidBooltest (BooleanXBooleany) { - //Arithmetic operators: - //!x=x*y; - //!x++; + //!x+=y; - // !... + //Relational and Logical A //!f (x>y) at // !... -F (x = =y); -F (x! =y); -F (!y); -x = x &&y; -x = x | |y; in //Bitwise Operators - //!x=~y; tox = x &y; + // ... - //!x=x<<1; the // !... * //Compound Assignment $ //!x+=y;Panax Notoginseng // !... - //!x<<=1; the // !... +X &=y; A // ... the //Casting + //!char c= (char) x; - // !... $ } $ - voidChartest (CharXChary) { - //Arithmetic Operators thex = (Char) (X *y); - // ...WuyiX + +; thex = (Char) +y; - //Relational and Logical WuF (x >y); - // ...; About //!f (!x); $ //!f (x&&y); - //!f (x| | y); - //Bitwise Operators -x = (Char) ~y; Ax = (Char) (X *y); + // ... thex = (Char) (x >>y); - // ... $ //Compound Assignment: theX + =y; the // ... theX <<= 1; theX &=y; - // ... in //Casting the //!boolean b1= (Boolean) x; the byteB = (byte) x; About Shorts = ( Short) x; the // ... the } the + //byte short int long similar - the voidFloattest (floatXfloaty) {Bayi //Arithmetic Operators thex = (x *y); the // ... -X + +; -x = +y; the //Relational and Logical theF (x >y); the // ...; the //!f (!x); - //!f (x&&y); the //!f (x| | y); the //Bitwise Operators the //!x = ~y;94 // !... the //Compound Assignment theX + =y; the // ...98 //!x<<=1 About // !... - //Casting101 //!boolean b1= (Boolean) x;102 byteB = (byte) x;103 Shorts = ( Short) x;104 // ... the }106 //Double similar107}
Note that the boolean is quite limited. Can assign to it the values true and false, and can test it for truth and falsehood, but can Not add Booleans or perform any other type of operation on them.
In char,Byte,and short, you can see the effect of promotion with the arithmetic operators. Each arithmetic operation in those types produces an int Result,which must is explicitly cast back to the Original type (a narrowing conversion that might lose information) to assign back to that type. with int values,however,you does not need to cast,because everything are already an int .
Java: Operations for each basic data type