Java - JavaSE - 语言基础

语言基础

算术运算符

注意:算法(第四版)中指出

+、-、*、/ 都是被重载过的

Java 语言规范规定,在逻辑运算符中,! 拥有最高的优先级,之后是 &&,接下来是 ||

%,如果对负数取模,可以把模数负号忽略不计。

System.out.println(5 % -2); //1
System.out.println(-5 % 2); //-1
System.out.println(-5 % -2); //-1
System.out.println(5 % 2); //1

基本数据类型

double d1 = 1.0;
double d2 = 0.00;
double d3 = d1 / d2;
System.out.println(d3); //output: Infinity
//System.out.println(d3 instanceof Double); //Error,instanceof 不能比较基本数据类型
System.out.println(d1 / d2); //output: Infinity

Double d1 = 1.0;
Double d2 = 0.00;
Double d3 = d1 / d2;
System.out.println(d3); //output: Infinity
System.out.println(d3 instanceof Double); //true
System.out.println(d1 / d2); //output: Infinity

分支语句

while & for 循环:开发使用 for 循环比较多,变量可以从内存中较早的消失?

可以,这也是局部代码块的优点。

public class LoopTest01 {
    /*
    实验:for 和 while 循环
    验证:for 循环,变量可以从内存中较早的消失
    结果:循环结束时,i 变量仍存在,j 变量已消失,验证结论
    */
    public static void main(String[] args) {
        int i = 3;
        while (i > 0) {
            System.out.println(i--);
        }

        for (int j = 0; j < 3; j++) {
            System.out.println(j);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/chenxianbin/p/11832418.html