2020/8/25
- short/char/byte三种类型在进行数学运算时,会先转换成int型再计算。如果值未超过左侧类型范畴,则java编译器会主动补充(short)(char)(byte)的强制转换。
- 编译器的运算优化:
short a = 1; short b = 2; short c = a + b 会报错,因为a,b在运算时都转换成int,间接赋值会报类型谬误。
short c = 1+2 不会报错,1,2仍为int型,java编译器在编译过程中主动将常量后果相加,.class文件中c的值已为3,之后在运行时,再对3进行强制类型转换。
然而当表达式中呈现变量时,编译器会报类型谬误。 - 运算符中有数据类型大的,后果以类型大的为准。
- 三元运算: 变量 = 条件判断 ? 变量A:变量B 例:int max = a>b ? a:b
- 应用准确的浮点数运算,可应用BigDecimal类
- boolean类型只占1bit
- 带标签的continue和break
outer:for(int i=0;i<10;i++) { for(int j=2;j<i/2;j++) { if(i % j == 0) { sum += j; continue outer; } } }
2020/8/26
- 堆、栈、动态区及内存剖析
package my.java;public class TestStu { int age; int id; String name; Computer comp; void study() { System.out.println("学习应用:"+ comp.brand); } void play() { System.out.println("玩游戏"); } public static void main(String[] args) { TestStu testStu = new TestStu(); testStu.id = 10; testStu.name = "小明"; testStu.age = 19; Computer comp = new Computer(); comp.brand = "surface"; testStu.comp = comp; testStu.study(); testStu.play(); }}class Computer{ String brand;}