0. In Java, integers are defaulted to type int, and floating-point numbers default to double types.
1. When defining a long integer, you must add the suffix L or L. such as long earthpeople=7_000_000_000l;
2. When defining a single precision (float), the suffix F or f must be added. such as float wage=2000.354f;
3. Conversions between numeric types: auto-convert (implicit conversion) casts (explicit conversions)
4. Convert Figure 6 Solid arrows for no information loss conversions, 3 dashed arrows for conversions that may have loss of precision
5. Implicit conversion: From small to large, data type automatically promoted
Byte,short,char -->int-->long-->float-->double
To put it another way: when you use two values for a two-dollar operation, you first convert the two operands to the same type before you calculate.
Conversion rules: (a) Two operands have one is a double type, and the other operand is converted to a double type;
(b) Otherwise, one of the operands is a float type, and the other operand is converted to float type;
(c) Otherwise, one operand is a long type, and the other operand is converted to a long type;
(iv) Otherwise, two operands will be converted to the INT type.
Such as
BYTE a=20;
int b=a;
Virtual Opportunity Converts the variable A to the right of the assignment operator to the int type, and then assigns the value to the left variable B.
Example 1:
Short a=10;
a=a+2;
a=a+ (short) 2; This will also be an error.
Compile-time error: Incompatible type: conversion from int to short can be a loss
Cause: A+2 will implicitly convert the result to the int type, assigning the int type to the left short type variable, there will be data loss, so the error
Approach: A= (short) (a+2);
Contrast
Short a=10;
a+=2;
This will not be an error.
a+=2, although can be regarded as a=a+2, but there is still a difference. There is a cast in a+=2, that is, a = (short)(a+2), the value of a+2 is cast to the short type, so there is no error.
Example 2:
Short a=20;
Short b=30;
B=a+b;
Also error, the reason and example 1 the same
It is important to note that when converting between integers, the values do not change. When converting integer types, especially large integer types, to floating-point types, there is a possibility of loss of data precision due to different storage methods.
Such as
int n=123456789;
float f=n; F=1.23456792e8
6. Explicit conversions
Format: (converted data type) variable or value;
In general, it is not recommended to use coercion type conversions at all.
Forcing a type conversion converts a floating-point value to an integer by truncating the fractional portion.
Such as
Double x=9.997;
int y= (int) x; The value of Y is 9
Example 3:
Byte b= (byte)130;
System.out.println (b); //Printing results -126
Reason:
Decimal number 130 is converted to binary: 10000010
represented in memory as
Original code: 00000000 00000000 00000000 10000010 because it is a positive number, the original code is the same as the anti-code complement
Complement: 00000000 00000000 00000000 10000010
To intercept the complement, take the last 8 bits (1 byte size): 10000010
10000010 is the complement. Because the sign bit is 1, the number is negative.
Anti-code:10000001--> original code 11111110
Convert to a decimal number of-126.
Type conversion for Java