Java: Operations for each basic data type

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.