类型转换
类型由低到高
byte - short,char - int - long - float -double
两种类型
1.强制转换:由 低->高
2.自动转换:由 高->低
需要注意的问题
1.不能对布尔值进行转换 2.不能把对象转换为不想干的类型 3.转换的时候可能存在内存溢出,或者是精度问题
数字分割
数字之间可以用_分割
int money = 10_0000_0000; System.out.println(money); //1000000000
操作数较大溢出问题
int years = 20; int total = money * years; System.out.println(total); //-1474836480 溢出 long total1 = money * years; System.out.println(total1); //-1474836480 因为在=右边结果在计算完的时候任然是int类型所以也是溢出 long total2 = money * (long) years; System.out.println(total2); //20000000000 在右边改变精度就可以了
位运算(面试可能会问)
/* * A = 0011 1100 * B = 0000 1101 * * A&B = 0000 1100 * A|B = 0011 1101 * A^B = 0011 0001 * ~B = 1111 0010 */
位运算技巧
/* * 0000 0000 0 * 0000 0001 1 * 0000 0010 2 * 0000 0011 3 * 0000 0100 4 * 0000 1000 8 * 0001 0000 16 * * 发现每向左边移动一位 就在前面的基础 *2 * 发现每向右边移动一位 就在前面的基础 /2 * 所以 << *2 * >> /2 * 效率高! * 问2*8怎么设计运算最快 * 2*8=2*2*2*2 */ System.out.println(2<<3);
字符串连接符
+号不同位置不同的结果
int a = 10; int b = 20; System.out.println(""+a+b); //1020 System.out.println(a+b+""); //30
变量的作用域
类变量
static修饰
实例变量
从属于对象;如果不自信初始化,这个类型默认值位0 0.0
布尔值false
局部变量
类里面的变量:必须要进行声明和初始化
命名
除了类名:其他都是首字母小写+驼峰命名