你知道这些程序出现这些结果的原因吗?

java 一些基础知识

public class Main {

    public static void main(String[] args) {
        System.out.println(3/0);
        //Exception in thread "main" java.lang.ArithmeticException: / by zero
        
        System.out.println(3.0/0);
        //Infinity
        
        test1();//no
        
        test2();//Exception in thread "main" java.lang.ArithmeticException: / by zero
        
        System.out.println(2.0 - 1.1);//0.8999999999999999

        System.out.println(5++);//编译异常
        System.out.println((byte)300);//44
        
        stringTest();
        
        int x = 0;
        ++x;
        System.out.println(x);//1
        
        int x2 = 0;
        x2++;
        System.out.println(x2);//1
        
        int x3 = 0;
        System.out.println(++x3);//1
        
        int x4 = 0;
        System.out.println(x4++);//0

    }

    private static void stringTest() {
        String s1 = "s1";
        String s2 = "s1";

        String s3 = s1 + s2;
        String s4 = "s1s1";
        String s5 = new String("s1");
        String s6 = new String(s1);
        String s7 = s1;

        System.out.println(s1 == s7);// true
        System.out.println(s2 == s7);// true
        System.out.println(s1 == s2);// true
        System.out.println(s3 == s4);// false
        System.out.println(s1 == s5);// false
        System.out.println(s1 == s6);// false
        System.out.println(s2 == s5);// false
        System.out.println(s2 == s6);// false
        System.out.println(s5 == s6);// false

    }

    private static void test1() {
        int x = 0;
        if (x != 0 && x / 0 > 2) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }
    }

    private static void test2() {
        int x = 0;
        if (x != 0 & x / 0 > 2) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值