java基础

2020/8/25

  1. short/char/byte三种类型在进行数学运算时,会先转换成int型再计算。如果值未超过左侧类型范围,则java编译器会自动补充(short)(char)(byte)的强制转换。
  2. 编译器的运算优化:
    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进行强制类型转换。
    但是当表达式中出现变量时,编译器会报类型错误。
  3. 运算符中有数据类型大的,结果以类型大的为准。
  4. 三元运算: 变量 = 条件判断 ? 变量A:变量B 例:int max = a>b ? a:b
  5. 使用精确的浮点数运算,可使用BigDecimal类
  6. boolean类型只占1bit
  7. 带标签的continue和break
outer:for(int i=0;i<10;i++) {
        for(int j=2;j

2020/8/26

  1. 堆、栈、静态区及内存分析
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;
}

java基础_第1张图片

你可能感兴趣的:(java)