Java里面有八种基本数据类型,在程序中都会存储在栈中。
整数型:byte short int long
字符型:char
浮点型:float double
Boolean型:boolean
整数型:byte short int long
byte:1个字节(8位bit)0
short:2个字节(16位bit)0
int:4个字节(32为bit)0
long:8个字节(64位bit)0L
字符型:char
char:2个字节(16位bit)’\u0000’显示为一个空格
浮点型:float double
float:4个字节(32位bit)0.0f
double:8个字节(64位bit)0.0
boolean型:boolean 1位bit false
数据类型之间的转换:强制类型转换和自动转换
Byte->short(char)->int->long->float->double 数值范围的大小决定是否需要强制类型转换
byte型的数据不能转换成char型,short型和char型数据也不能相互转换
注意:char字符转换为int类型的时候,其值为Unicode码对应的数值。
浮点型转换为整数型的时候,不是四舍五入,是直接截取(1.2,1.7都是直接转为1)
BigInteger, BigDecimal内部存储机制是数组。在进行操作运算的时候需要调用类中的方法,如add,subtract等。
包装类,这里需要注意的就是自动装箱和解箱。类对象不能直接使用二元运算,但是包装类对象就会用到自动解箱的操作,从而解决这个问题。
public class Test{
public static void main(String[] args) throws Exception{
Integer i=1;//自动装包
Integer j=2;
int k=i+j;//解包后做运算
System.out.println(k);
}
}