Java基础

Java编程思想笔记3.操作符

2018-11-09  本文已影响2人  卢卡斯哔哔哔

点击进入我的博客

3.1更简单的打印语句

System.out.println("imbug");

通过编写一个小类库,并通过import static该方法来实现简化打印(基本没啥用)。

public class Print {
    public static void println(String str) {
        System.out.println(str);
    }
}
import static s1.Print.println;

public class Test {
    public static void main(String[] args) {
        println("imbug");
    }
}

3.2 使用Java操作符

3.3 优先级

运算符优先级表.png

3.4 赋值

赋值=的意思是取右边的值把它复制给左边。
左边的值必须是明确的、已命名的变量。
右值可以是任何常熟、变量或者表达式(只要它能生成一个值)。
基本类型传递的是值,对象类型传递的是引用。

3.5 算术操作符

+-*/%
/:整数的除法会直接干掉小数位而不是四舍五入
+-:还可以用作正负号

3.6 自动递增和递减

++--
前缀式:++a先执行运算再生成值,即++a == a is true
后缀式:a++先生成值再执行运算,即a == a++ is true

3.7 关系运算符

><>=<=!===

数值的包装类

    Integer x1 = new Integer(10);
    Integer x2 = new Integer(10);
    System.out.println("x1 == x2: " + (x1 == x2));
    System.out.println("x1.equals(x2): " + x1.equals(x2));
    // x1 == x2: false
    // x1.equals(x2): true
    Integer y1 = 10;
    Integer y2 = 10;
    System.out.println("y1 == y2: " + (y1 == y2));
    System.out.println("y1.equals(y2): " + y1.equals(y2));
    // y1 == y2: true
    // y1.equals(y2): true
    Integer z1 = 1000;
    Integer z2 = 1000;
    System.out.println("z1 == z2: " + (z1 == z2));
    System.out.println("z1.equals(z2): " + z1.equals(z2));
    //z1 == z2: false
    //z1.equals(z2): true

上述代码的.class文件反编译后的代码

    Integer x1 = new Integer(10);
    Integer x2 = new Integer(10);
    System.out.println((new StringBuilder()).append("x1 == x2: ").append(x1 == x2).toString());
    System.out.println((new StringBuilder()).append("x1.equals(x2): ").append(x1.equals(x2)).toString());
    Integer y1 = Integer.valueOf(10);
    Integer y2 = Integer.valueOf(10);
    System.out.println((new StringBuilder()).append("y1 == y2: ").append(y1 == y2).toString());
    System.out.println((new StringBuilder()).append("y1.equals(y2): ").append(y1.equals(y2)).toString());
    Integer z1 = Integer.valueOf(1000);
    Integer z2 = Integer.valueOf(1000);
    System.out.println((new StringBuilder()).append("z1 == z2: ").append(z1 == z2).toString());
    System.out.println((new StringBuilder()).append("z1.equals(z2): ").append(z1.equals(z2)).toString());
通过new Integer新建对象
自动装箱与拆箱
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

备注内容:可以从https://varaneckas.com/jad/下载Java反编译工具JAD;或者可用从http://jd.benow.ca/下载反编译工具JD-GUI

3.8 逻辑操作符

&&||!

布尔值短路
    public static boolean func1() {
        System.out.println("func1");
        return true;
    }

    public static boolean func2() {
        System.out.println("func2");
        return false;
    }

    public static boolean func3() {
        System.out.println("func3");
        return true;
    }
    if(func1() && func2() && func3());  // 不执行func3中的内容
    if(func1() || func2() || func3());  // 不执行func2和fanc3中的内容
    // 不使用if来控制该流程
    if(func1()) {
        func2();
    }
    // 方法
    boolean var = func1() && func2();

3.9 直接常量

3.10 按位操作符

&|^
&=|=^=

3.11 移位操作符

<<:高位(包括符号位)舍弃,低位补零。
>>:有符号右移运算,若高位是1,则高位补1;若高位是0,则高位补0。
>>>:无符号右移,最高位补0。

<<=>>=>>>=

    short s = -1;
    System.out.println(Integer.toBinaryString(s));
    // 11111111111111111111111111111111
    System.out.println(Integer.toBinaryString(s >>> 10));
    // 1111111111111111111111
    s >>>= 10;
    System.out.println(Integer.toBinaryString(s));
    // 11111111111111111111111111111111

3.12 三目运算符

boolean-exp ? val1 : val2

3.13 字符串运算符

++=

3.14 操作符常犯错误

3.15 类型转换操作符

3.16 没有sizeof

Java所有基本数据类型的size是确定的,所以不需要sizeof

3.17 操作符小结

        short s1 = 10;
        short s2 = 10;
        // short s3 = s1 + s2;
        // short s4 = s1 >> 1;
上一篇 下一篇

猜你喜欢

热点阅读