Section fourth (conversion of basic types)

Source: Internet
Author: User

/*about shaping data in the Java language: Byte 1byte (8bit) -128 ~ 127 short 2byte int 4byte long 8byte integer type There are three ways to express 1. Decimal 2. Octal begins with 0 in 3. Hex starts with 0x*/ Public classdatatypetest01{ Public Static voidMain (string[] args) {//decimal        inti =Ten;//Common Types//octal        intj =010; //hexadecimal        intK =0x10; System. out. println ("i ="+i); System. out. println ("j ="+j); System. out. println ("k ="+k); //assigns an int type 100 literal value to the M variable        intm = -; //assigns an int type 150 literal value to a long type N//capacity of type int is less than type long//The data class conversion already exists by default, but is "automatic type conversion"        Longn = Max; //because "L" appears, 160 is considered a long type.//The following program does not have a type conversion, just a normal assignment operation        Longx =160L; //you can convert an int type to a long type "automatic type conversion"        Longy =234233633;//If the value of type int is less than 2^31-1//when to use "L" or "L", it is recommended to use "L "//if the value is greater than 2^31-1, use L and is a long type        Longt =8797979L; System. out. println ("L ="+t); }}
/*byte type*/ Public classdatatypetest02{ Public Static voidMain (string[] args) {Longi =1000L; //cannot auto type conversion//you need to force type conversions, add mandatory type conversions ()//after adding (int), the compilation passes, but the program may lose precision at run time, so be sure to use it carefully//large-capacity long-like small-volume conversion requires forced type conversion        intj = (int) I; //byte value range (-128 ~ 127) As long as the range is not exceeded        byteK =Ten; bytem =127; //byte n = 128; Error wording out of range//Short value range ( -32768 ~ 32767)         Shortx =23423; //Char Value range (0 ~ 65535)//Summary: If the integer does not exceed the Byte,short,char range, you can assign the integer value to Byte,short,char        Chary = -; CharU =65535; intv =58787; CharW = (Char) v; System. out. println ("j ="+ j +"u ="+ U +"W ="+W); }}
/*about floating-point float 4 bytes double 8 bytes*/ Public classdatatypetest03{ Public Static voidMain (string[] args) {Doublei =1.2; //Forcing type conversions        floaty = (float) I; //Automatic assignment operator        floatx =1.2F; System. out. println ("i ="+ i +"y ="+ y +"x ="+x); }}
/*Boolean types in Java 1. There are only two values: True/false not 0 and 1 2. Mainly used in logical operators and conditional control statements*/ Public classdatatypetest04{ Public Static voidMain (string[] args) {//Boolean i = 1;Boolean sex=true; if(Sex) {System. out. println ("Men"); } Else{System. out. println ("Women"); } System. out. println ("sex ="+sex); }}
/*Byte,short,char do mixed operations, you need to convert to int before doing the operation.*/ Public classdatatypetest05{ Public Static voidMain (string[] args) {bytei =Ten;  Shortj = -; intK = i +J; bytem = (byte) (i +j); System. out. println ("k ="+ K +"m ="+m); /*Java programs run at the time of two times 1. Compile period 2. Run time at compile stage just know i+j is int type int type cannot be assigned directly to byte type*/                byten = -; }}

Section fourth (conversion of basic types)

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.