Java Learning Note (i)-Basic data types

Source: Internet
Author: User

Basic data types

Basic data Type-fixed-point type

Package com.itany.basejava.day02.demo01;/* Basic data Type-fixed-point type */ Public classtest{ Public Static void Main(string[] args) {//1-Integer constant value the default type is int, and if the declared variable is of type Byte,short,char, the following are the three types of variables        //assignment, if the constant value is within the range represented by these three variables, then the constant of type int is automatically converted to Byte,short,char and assigned a value.         byteb =Ten; Shorts = -;//Byte BB = 128;//error, 128 exceeds the range indicated by byte, and at this point, 128 constants of type int cannot be automatically converted to byte type//Short SS = 65535;//type mismatch:cannot convert from int to short        //2-If you use an integer constant to assign a value to a variable of type long, the integer constant is automatically converted to a long type.         LongLL = $;//At this point, the 200 constant is of type int, and the int constant is automatically converted upward to a long type and then assigned        //After an integer constant with L or L, the constant represents a long type of constant, stored in 8 bytes.         LongL = +L//3-If the value of the constant exceeds the range represented by the int type, an error is made. //int xx = 11111111111111111;        Longxx =11111111111111Linti = -; }}

Basic data Type-char type

/ * Basic Data type-char */ Public classtest{ Public Static void Main(string[] args) {CharC1 =' A ';//char C2 = ';//required character constant must be '//char C3 = ' abc ';        CharC4 =' Medium ';The //characters are stored in memory and also in numeric values.         //integer constant 97 is within the range indicated by Char, then 97 is automatically converted to a char type, assigned value.         CharC5 = the; System. out. println (C5);//char types Since they are stored numerically, they can participate in numeric operations. If you are participating in a meta-calculation, use a numeric value corresponding to the character        intx = c1 +1; System. out. println (x);//escape character----\ character        //Special characters---ordinary characters        //' "\System. out. println (' \ '); System. out. println (' \ \ '); System. out. println ("\"");//Ordinary character---special meaning characterSystem. out. println ("xxxxxxx"); System. out. println ("AAAAAA\NBBBBBBBBBB"); System. out. println ("Xxxxxx\tyyyyyyyy");//\r carriage return, \ n newline, \ t tab}}

Basic data type-floating point, Boolean

/* Basic data type-float type, Boolean */ Public classtest{ Public Static void Main(string[] args) {///definition of floating-point variables        DoubleD =10.0;//Float FF = 10.0;//type mismatch:cannot convert from double to float        floatf =10.0F//10.0f or 10.0F indicates that the constant is of type float.         //floating-point representation        DoubleDD1 =10.0;DoubleDD2 =1.0e2;//Scientific counting method        DoubleDD3 =1.0E23;//1.0*10 23-Time Square        DoubleDD4 =1.0e-23;DoubleDD5 =. 12e-10;DoubleDd6 = DD1;The //java cannot represent 1/10, and if you need to specify a decimal number, use the BigDecimal classSystem. out. println (1.0-0.9);//boolean, default = FalseBoolean flag =true; Flag =false; }}
Special type of reference

Special Reference type-string

/ * Special Reference type-string */ Public classtest{ Public Static void Main(string[] args) {String S1 =""; String s2 ="a"; String s3 ="ABCdef";inti =Ten; String s ="ABC"; i = -;//re-assigns a value to the memory space that I point tos ="EFG"; System. out. println (i);//100System. out. println (s);//EFG}}
Type conversion rules

Type conversion rules: basic data types

/* Type conversion rule: basic data type */ Public classtest{ Public Static void Main(string[] args) {//1-boolean cannot be converted with any basic data type        //2-byte,short,char generally do not convert to each other. If you participate in another operation, it is automatically converted to the int type before the operation.        byteB1 =Ten; ShortS1 = -;CharC1 =' A ';byteB2 =Ten;byteB3 = -;//byte B4 = 30;//30 is constant, in the range represented by byte type, so int is automatically converted to byte        //B2 and B3 are automatically converted to type int and then added. At this point, even if the result is within the range indicated by byte, it cannot be automatically converted to a byte type        //Because the variables are added at this time, the result is indeterminate. //byte b4 = b2 + B3;//int sum1 = B1 + 1;        intsum = B1 + s1 + C1;//3-Any basic data type, it can be added to a string. The base type is first converted to a string and added to the string type.String str1 ="AAA"; String str2 ="BBB"; String STR3 ="CCC";//String through +, for stitching. String str = str1 + str2 + str3; System. out. println (str);//AAABBBCCC//System.out.println (STR-STR1);        intI2 = +;LongL2 =11111LfloatF2 =10.0FDoubleD2 =20.123; BooleanBOOL=true;CharC2 =' A '; System. out. println ("Ten"+ I2);//"Ten" + "101000 "System. out. println (str1 + i2);//System. out. println (str1 + L2); System. out. println (str1 + F2); System. out. println (str1 + D2);//"AAA" + "20.123"System. out. println (str1 +BOOL);//"AAA" + "True ", "Aaatrue"System. out. println (str1 + C2);//"AAA" + "a"->aaaa}}

Basic Data type Conversions

/ * Basic Data type conversion */ Public classtest{ Public Static void Main(string[] args) {//4-Type Auto-conversion (implicit conversion): Small number of bytes can be automatically converted into large number of bytes.         byteB1 =Ten; ShortS1 = B1;intI1 = S1;LongL1 = I1;floatf1 = L1;DoubleD1 = L1;//5-type CAST (explicit conversion), if the number of bytes is converted to small, the conversion must be cast. Cast, data overflow may occur, causing errors. //Type1 t = (type1) value;        byteB2 = (byte) I1;intI2 = (int) L1;//Note If data overflow is caused.         inti3 = $;byteB3 = (byte) i3;//If a floating-point data is converted to an integer type, a truncated operation occurs.         floatF2 =10.123FDoubleD2 =1000.6783;intII1 = (int) F2;intIi2 = (int) D2; System. out. println (II1); System. out. println (II2);//6-Multiple data type blending operations, small types are automatically converted to large types.         byteX1 =Ten; Shortx2 = -;intx3 = -;Longx4 = +;floatX5 =10.0FDoublex6 =20.5;CharX7 =' A '; System. out. println (x1 + x2 + x3 + x7 + x4 + x5 + x6); }}

Java Learning Note (i)-Basic data 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.