myJavaNotes Package Penalty for;
/ **
* automatic type conversion
* @ author Kang Dad
* @ date: January 21 2020 3:32:08 AM
*
* /
/ **
* cast
* @ author Kang Dad
* @ date: January 21 2020 9:04:41 PM
*
* /
/ **
* overflow
* @ author 123
* @ DATE: January 21 2020 PM 9:58:50
*
* /
public class TypeConversion {
/ **
@param args *
* /
public static void main (String [] args) {
// the TODO Auto-Generated Method Stub
// automatic type conversions, a small capacity type data can be automatically converted bit data type capacity
size // capacity refers to the range indicated.
// Example: long type (8 bytes) can be automatically converted bit float (. 4 bytes) type, i.e., long type variable can be assigned to a float
// Other types require the use of cast type () /
**
* char
* ↓
* byte Short → → → long↙ int. . .
.. * ↘
* V V
* → a float Double
*
*
* int automatically converted to short, byte, char type, need not be cast, without departing short, byte, char type can range
* Example: short b = 12; legal
* short b = 1234567; illegal, if it exceeds the range 1234567-short
* * /
int = 34 is a;
long a = B; // valid, int type automatic type long
double d = a; // legitimate , int type is automatically converted to double
// float f = 1.0; // illegal, the latter value = type double, it does not automatically translate to float
. 3 F = float;
D = F; // valid, a float automatically converted to double
f = a; // valid, int type is automatically converted to float, but will lack accuracy
f = b; // legitimate, long type is automatically converted to float, but will lack precision
d = b; // legitimate, long type is automatically converted to double precision ,, but there absence (dotted line)
// exception
char q = 123; // the right of the equal sign char to an int type of representation and not exceeding the range, automatically converted to char type
byte w = 123; // the right of the equal sign is not exceeded an int type byte indicates a range, automatically converted to byte type
short e = 123; / / right hand side is not exceeded an int type short range represented automatically converted into type short
// but not to char, byte, short populated with the variables, constants can only
Example //: q = a; a is int type variables to char type assignment q is illegal
// q = a; // illegal
System.out.println ( "===================== ========================== ");
/ *
* mandatory conversion: (of the type) variable
*
* * /
System.out.println (Q); // Output char type == Q '{'
System.out.println ((int) Q); // char to an int type cast
System.out.println ( "================================================ == ");
/ **
* 1, overflow: when operating large numbers, pay attention to whether an overflow (result is greater than the range to be represented, or negative)
* 2, try not to name names of variables l, l easily confused with the number 1.
Constant the right * 3, long type variable in the assignment, = equal sign at the end of the end as far as possible with a capital L, not small L
* * /
int Money = 1000000000; // 10 Yi
int years = 20;
int Total = Money * years;
System.out.println ( "Total =" Total +); // total returned when negative, is beyond the scope of int overflow;
Long total1 = Money * years;
System.out.println ( "total =" + total ); // return a negative number when toutal any course. Money default int years and, therefore money * int result of type int, long converted to type int type but has overflowed in the process, data loss;
long total2 Money * = (long) years;
the System.out .println (total2); total2 == 20000000000 correct // returned. years first converted to long type, and an int * type long long type of result, it does not overflow, the data is not lost, the result is correct
// naming
int l = 2; // is not easy to distinguish or lowercase L 1
Long S = 2345l; // end is not easy to distinguish lowercase L or 1
}
}
Java study notes - the type of automatic conversion, casts overflow
Guess you like
Origin www.cnblogs.com/destiny-2015/p/12233673.html
Recommended
Ranking