zoukankan      html  css  js  c++  java
  • Null 规格严格

    In the first case, the compiler knows that you're trying to unbox a compile-time constant of null.

    In the second case, the type of the conditional expression is Integer, so you're effectively writing:

    Integer tmp = new Random().nextBoolean() ? 1 : null;
    return (int) tmp;

    ... so the unboxing isn't happening on a constant expression, and the compiler will allow it.

    If you changed it to force the conditional expression to be of type int by unboxing there, it would fail:

    // Compile-time failure
    return new Random().nextBoolean() ? 1 : (int) null;


    Boxing partially hides the distinction between primitives and corresponding wrapper objects, but it doesn't remove it.

    There are two distinctions which are not changed by boxing:

    • objects can be null, while primitives cannot
    • objects have both state and identity, while primitives have only state (the value)

    Occasionally, these differences can cause problems when using boxing.

    Some points to remember :

    • be careful with nulls. Auto-unboxing a null object will cause a NullPointerException.
    • comparing items with == and equals must be done with care.


  • 相关阅读:
    啃掉的博文全记录
    DP五十题
    noip 真题班刷题记录及总结思考
    dfklsJj
    【2018.11.7】luogu NOIp热身赛 及刷题思考
    【trie树专题】
    【倍增专题】
    10.23
    简析 NP 问题 和P问题
    [NOIP 2010普及组 No.4] 三国游戏
  • 原文地址:https://www.cnblogs.com/diyunpeng/p/2684825.html
Copyright © 2011-2022 走看看