Type conversion of basic Java data

Source: Internet
Author: User

Automatic type promotion1.boolean does not allow type conversion Boolean type Boolean occupies a byte, because of its own special meaning, so that other basic types can not be a type of conversion (neither an automatic type of promotion, nor coercion type conversion), otherwise the compilation error. 2. Type with a small range of values-a type with a large range of values, the JVM is responsible for automatic promotion of numeric types during compilation
 + ;    //Compile error Type mismatch:cannot convert from int to byte
1.5 ; //Compile error Type Mismatch:cannot convert from double to float
3 ; //compile correctly
the first two sentences of "exception" are well understood, but the int assignment byte type compiles correctly because:int→ (byte/char/short) type conversions do not need to be cast under certain conditionsJVMIn the compilation process, the default value of type int is assigned to a numeric type variable that is smaller than the value range of type int (this unification is known as the numeric type k,k can be a byte/char/short type), and if this int value exceeds the range of K, then a direct compilation error occurs. However, if the value of this int is still within the value type K range, the JVM will customize an implicit type conversion to convert this int value to type K. 3.byte→char,char→short does not perform automatic type promotion because of the range of values, the type conversion between Byte, short and char is not possible, but byte can be promoted to the short type. 4. Default type of data in Java, Integer type (byte/short/int/long) defaults to int for undeclared data typetypeThe floating-point type (float/double) defaults to the double type.
10000000000
10000000000 The default data type is int, and the numeric range of int is -2^31 ~ 2^31-1, obviously out of range, so compile an error, here you only need to declare the native default type. -----long a = 10000000000L; Compile correctly
Casting of numeric types 1. Forcing type conversions to convert a numeric type to a lower range may cause loss of precision
3//compile correctly: int to byte implicit type conversion occurs during compilation
3 ;
b = a//Compile Error: cannot convert from int to byte
c = (byte) a//compile correctly
D = (float4.0;
Analysis: B=a compilation error is becauseA is a variable, and the compilation period cannot be determined. 2. Principle analysis of forced type conversion
233 ;
b = (byte) a;
System. out. println (+ b); //output: -23
Analysis: The binary representation of 233 is: 24-bit 0+11101001,byte only 8 bits,To abandon from the highGet: 11101001, because the highest bit is the sign bit, 1 is a negative number, and its corresponding value is represented as-23. 3. When you perform a mathematical operation, the data type is automatically promoted to the larger of the operator's left and right
Ten;
g = e + F//Compile error + direct 10 and 11 type promotion for int
h = (byte) (E + f); //compile correctly
The relevant underlying principle of the precision of the strong transfer loss can be seen in the Java &0XFF Usage analysis and the original code, anti-code, complementary knowledge 



From for notes (Wiz)



Type conversion of basic Java data

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.