package cn.nrsc.demo01;
/*
* 变量的数据类型转换: (了解)
* 强制数据类型转换:
* 小的数据类型 变量名 = (小的数据类型)大的数据类型的值或者变量
* byte < short, char < int < long < float < double
*
* 占用字节: 1 2 2 4 8 8 8
*
* 隐式数据类型转换:
* 大的数据类型 变量名 = 小的数据类型的变量或者值
*
*
* 注意点: 字符的底层是以ASCII表向对应的整数来进行存储的;
* byte, short, char 再进行运算的时候会自动提升为int类型之后来进行运算
* byte 1 -128~127
* short 2 -32768~32767
* int(默认) 4 -2147483648~2147483647
* long 8 -263~263-1
* float 4 -3.403E38~3.403E38
* double(默认) 8 -1.798E308~1.798E308
* char 2 表示一个字符,如('a','A','0','家')
* boolean 1 只有两个值true与false
*/
public class Demo01BianLiang {
public static void main(String[] args) {
// 强制数据类型转换
int a = (int) 100L;
double d = 6.66;
a = (int) d;
System.out.println(a);// 6
// 隐式数据类型转换
long l = 200;
float f = l;
System.out.println(l);// 200
System.out.println(f);// 200.0
// 注意点:
char c = 'a';
System.out.println(c);// a
System.out.println((int) c);// 97
int i = c;
char c2 = 98;
System.out.println(c2);// b
// 运算
char c3 = (char) (c2 + 'a'); // 195
System.out.println(c3);// ? 没有对应的ascii的值向对应了.
int i2 = c2 + 'a';
System.out.println(i2);// 195
}
}
JAVA 强制数据类型转换和隐式数据类型转换
最新推荐文章于 2024-09-19 05:10:41 发布