Java 기본 데이터 유형 간 변환
3090 단어 Java 기반
public class DataTypeChangeTest {
private static byte byteV;
private static short shortV;
private static int intV;
private static long longV;
private static float floatV;
private static double doubleV;
private static boolean booleanV;
private static char charV;
static {
byteV=120;
shortV=200;
intV=-40000;
longV=System.currentTimeMillis();
floatV=311.22222F;
doubleV=311.111777111777111D;
booleanV=false;
charV='d';
}
public static void main(String[] args) {
testByte();
}
/**
* byte
* 1.byte short int long float double
* 2. byte char
* 3. byte boolean
* 4. short int long float double float char byte
* 5. short int long float double float char byte -128 ~127
* 256
* 8 1 ( +1)
*/
private static void testByte() {
short shortVV=byteV;
System.out.println(String.format("byte short:%s,%s",byteV,shortVV));
int intVV=byteV;
System.out.println(String.format("byte int:%s,%s",byteV,intVV));
long longVV=byteV;
System.out.println(String.format("byte long:%s,%s",byteV,longVV));
float floatVV=byteV;
System.out.println(String.format("byte float:%s,%s",byteV,floatVV));
double doubleVV=byteV;
System.out.println(String.format("byte double:%s,%s",byteV,doubleVV));
char charVV= (char) byteV;
System.out.println(String.format("byte char:%s,%s",byteV,charVV));
byte byteV1= (byte) shortV;
System.out.println(String.format("short byte:%s,%s",shortV,byteV1));
byte byteV2= (byte) intV;
System.out.println(String.format("int byte:%s,%s",intV,byteV2));
byte byteV3= (byte) longV;
System.out.println(String.format("long byte:%s,%s",longV,byteV3));
byte byteV4= (byte) floatV;
System.out.println(String.format("float byte:%s,%s",floatV,byteV4));
byte byteV5= (byte) doubleV;
System.out.println(String.format("double byte:%s,%s",doubleV,byteV5));
byte byteV6= (byte) charV;
System.out.println(String.format("char byte:%s,%s",charV,byteV6));
// boolean ag=a;
}
}
이제 요약해 보겠습니다.