zoukankan      html  css  js  c++  java
  • java趣题

    try、catch面试题

    ++计算

    int a = 2;
    int b = (a++) << (++a) + (++a);
    System.out.println(b + " " + a);

    类似a++,++a这样的表达式,值不会边,但是a会变。即:b = 2 << (4 + 5) = 1024

    a = 5

    try catch中的return finally

    先执行catch,再finally

    public static int xxx(int i) {
            try {
                throw new Exception("异常");
            } catch (Exception e) {
                System.out.println("return.....");
                return i;
            } finally {
                System.out.println("finally.....");
            }
        }
    
        public static void main(String[] args) {
            System.out.println("数值i:" + xxx(0));
        }

    return.....
    finally.....
    数值i:0

    最后返回finally中的return

    public static int xxx(int i) {
            try {
                throw new Exception("异常");
            } catch (Exception e) {
                System.out.println("return.....");
                return 2;
            } finally {
                System.out.println("finally.....");
                return 3;
            }
        }
    
        public static void main(String[] args) {
            System.out.println("数值i:" + xxx(0));
        }
    return.....
    finally.....
    数值i:3
    
    

    catch中return变量,finally变量计算

    public static int xxx(int i) {
            try {
                throw new Exception("异常");
            } catch (Exception e) {
                System.out.println("return.....");
                return i;
            } finally {
                ++i;
                System.out.println("finally.....");
            }
        }
    
        public static void main(String[] args) {
            System.out.println("数值i:" + xxx(0));
        }

    return.....
    finally.....
    数值i:0

     

    因为这里是值传递,在执行return前,保留了一个i的副本,值为0。

    然后再去执行finally,finally完后,到return的时候,返回的并不是当前的i,而是保留的那个副本,也就是0.所以返回结果是0。

    catch、finally都return

    public static int xxx(int i) {
            try {
                throw new Exception("异常");
            } catch (Exception e) {
                System.out.println("return.....");
                return i;
            } finally {
                System.out.println("finally.....");
                ++i;
                return i;
            }
        }
    
        public static void main(String[] args) {
            System.out.println("数值i:" + xxx(0));
        }

    return.....
    finally.....
    数值i:1

    如果finally中有return,不会走catch中的return。

    小结

    1、在catch中有return的情况下,finally中的内容还是会执行,并且是先执行finallyreturn

    2、需要注意的是,如果返回的是一个基本数据类型,则finally中的内容对返回的值没有影响。因为返回的是 finally执行之前生成的一个副本。

    3、当catchfinally都有return时,return的是finally的值。

  • 相关阅读:
    微前端开发常见问题汇总
    什么是UUId?理解UUId的五个版本和使用
    前端实现.md文件转换成.html文件
    使用HTML5 SVG 标签
    小程序开发中遇到的问题
    Django REST framework+Vue 打造生鲜超市(十一)
    Django REST framework+Vue 打造生鲜超市(十)
    Django REST framework+Vue 打造生鲜超市(九)
    Django REST framework+Vue 打造生鲜超市(八)
    Django REST framework+Vue 打造生鲜超市(七)
  • 原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/12762034.html
Copyright © 2011-2022 走看看