Java常见的面试题(一)

目录

一、判断是否是奇数

二、浮点数相减

三、字符串相加和字符相加

四、字符数组

五、增量问题

六、异常处理问题1

七、异常处理问题2

八、长整除

九、随机数的问题

十、整数边界的问题


public class Demo01_是否是奇数 {
    public static void main(String[] args) {
        for (int i = -5; i < 5; i++) {
            System.out.println(i + ":" + isOdd(i));
        }
    }

    //容易把负数的情况遗漏
    public static boolean isOdd(int num) {
        return num % 2 != 0;
    }
}

二、浮点数相减

public class Demo02_浮点数相减 {
    public static void main(String[] args) {
        //会出现丢失精度的问题
        System.out.println(2.0 - 1.1);//0.8999999999999999
    }
}

三、字符串相加和字符相加

public class Demo03_字符串和字符相加 {
    public static void main(String[] args) {
        System.out.println("H" + "e");//He
        //字符相加会先转换为int类型在相加
        System.out.println('H' + 'e');//173   
    }
}

四、字符数组

public class Demo04_字符数组 {
    public static void main(String[] args) {
        char[] chs = {'a', 'b', 'c'};
        //char数组直接打印会输出char数组的值  println(char[] chars)
        System.out.println(chs);//abc
        //和字符串相加会调用println的重载方法 println(String s)
        System.out.println("hello" + chs);//hello[C@49e4cb85
    }
}

五、增量问题

public class Demo05_增量问题 {
    public static void main(String[] args) {
        int j = 0;
        for (int i = 0; i < 100; i++) {
            j = j++;
        }
        System.out.println(j);//0
        
    }
}

六、异常处理问题1

public class Demo06_异常处理问题 {
    public static void main(String[] args) {
        try {
            System.out.println("hello");
            System.exit(0);
        } finally {
            System.out.println("world");
        }
        //会输出hello。exit方法会强制终止程序
    }
}

七、异常处理问题2

public class Demo07_异常处理问题2 {
    public static void main(String[] args) {
        //程序会最终执行finally里面的语句
        System.out.println(panDuan());//false
    }

    public static boolean panDuan() {
        try {
            return true;
        } finally {
            return false;
        }
    }
}

八、长整除

public class Demo08_长整除 {
    public static void main(String[] args) {
        final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
        final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
        //24*60*60...是int类型相乘,会发生数据精度丢失
        System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);//5
        System.out.println("----------------");
        final long MICROS_PER_DAY1 = 24l * 60 * 60 * 1000 * 1000;
        final long MILLIS_PER_DAY1 = 24l * 60 * 60 * 1000;
        //应该先转换为long类型在相乘
        System.out.println(MICROS_PER_DAY1 / MILLIS_PER_DAY1);//1000
    }
}

九、随机数的问题

public class Demo09_随机数的问题 {
    public static void main(String[] args) {
        Random r = new Random();
        StringBuilder sb = null;
        switch (r.nextInt(2)) {
            case 1:
                sb = new StringBuilder('H');
            case 2:
                sb = new StringBuilder('E');
            default:
                sb = new StringBuilder('L');
        }
        sb.append("l");
        sb.append("o");
        System.out.println(sb);

        //问题1:r.nextInt(2)取不到2这个值
        //问题2:case 1和case 2后面没有加break;
        //问题3:new StringBuilder('H')里面参数传的是字符类型,会自动转为int,调用的构造方法为指定
        //      容器大小的方法
    }
}

十、整数边界的问题

public class Demo10_整数边界问题 {
    public static void main(String[] args) {
        int end = Integer.MAX_VALUE;
        int start = end - 100;
        int count = 0;
        //会造成死循环,当i等于end时在+1会变成-2147326492,永远小于end
        for (int i = start; i <= end; i++) {
            count++;
            System.out.println(i);
        }
        System.out.println(count);
    }
}

猜你喜欢

转载自blog.csdn.net/fy_java1995/article/details/81976239