关于java:Boolean和其基本类型boolean的区别

6次阅读

共计 2506 个字符,预计需要花费 7 分钟才能阅读完成。

hello,大家好,这里是可傥。明天咱们来说一下根本数据类型 boolean 和其包装类 Boolean。
明天咱们联合以下例子来阐明这两者的区别。

public class BooleanTest {
    private Boolean isTrue;

    /**
     * Getter method for property<tt>isTrue</tt>.
     *
     * @return property value of isTrue
     */
    public Boolean getTrue() {return isTrue;}

    /**
     * Setter method for property <tt>isTrue</tt>.
     *
     * @param isTrue value to be assigned to property isTrue
     */
    public void
    setTrue(Boolean isTrue) {this.isTrue = isTrue;}

    public static void main(String[] args) {
        //boolean a = null;
        Boolean a = null;
        Boolean b = new Boolean(null);
        Boolean c = new Boolean("true");
        if (null ==a){a =true;}
        System.out.println(a);
        System.out.println(b);
        BooleanTest booleanTest = new BooleanTest();
        booleanTest.setTrue(null);
        System.out.println(booleanTest.isTrue);
        System.out.println(booleanTest.getTrue());
    }
}

一、是否能够为 null

Boolean 因为是一个类,所以他的援用对象能够为 null;
Boolean a= null 或者 Boolean b = new Boolean(null)都是容许的,而根本数据类型 boolean 只能为 true or false。

二、Boolean 能够 new 中引入字符串

要晓得 Boolean 为什么能够 new 中退出字符串,咱们就关上 Boolean 的源码,源码是这样的:


    /**
     * Allocates a {@code Boolean} object representing the value
     * {@code true} if the string argument is not {@code null}
     * and is equal, ignoring case, to the string {@code "true"}.
     * Otherwise, allocate a {@code Boolean} object representing the
     * value {@code false}. Examples:<p>
     * {@code new Boolean("True")} produces a {@code Boolean} object
     * that represents {@code true}.<br>
     * {@code new Boolean("yes")} produces a {@code Boolean} object
     * that represents {@code false}.
     *
     * @param   s   the string to be converted to a {@code Boolean}.
     */
    public Boolean(String s) {this(parseBoolean(s));
    }

    /**
     * Parses the string argument as a boolean.  The {@code boolean}
     * returned represents the value {@code true} if the string argument
     * is not {@code null} and is equal, ignoring case, to the string
     * {@code "true"}. <p>
     * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
     * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
     *
     * @param      s   the {@code String} containing the boolean
     *                 representation to be parsed
     * @return     the boolean represented by the string argument
     * @since 1.5
     */
    public static boolean parseBoolean(String s) {return ((s != null) && s.equalsIgnoreCase("true"));
    }

这边就能够很显著的看到,字符串唯有 ”true” 且不为 null 的时候才是 true(true 不辨别大小写),其余全副为 false。

三、阿里巴巴 Java 开发手册

* 对于根本数据类型与包装数据类型的应用规范如下:
1)【强制】所有的 POJO 类属性必须应用包装数据类型。
2)【强制】RPC 办法的返回值和参数必须应用包装数据类型。
3)【举荐】所有的局部变量应用根本数据类型。
阐明:POJO 类属性没有初值是揭示使用者在须要应用时,必须本人显式地进行赋值,任何 NPE 问题,或
者入库查看,都由使用者来保障。
正例:数据库的查问后果可能是 null,因为主动拆箱,用根本数据类型接管有 NPE 危险。
反例:某业务的交易报表上显示成交总额涨跌状况,即正负 x%,x 为根本数据类型,调用的 RPC 服务,调
用不胜利时,返回的是默认值,页面显示为 0%,这是不合理的,应该显示成中划线 -。所以包装数据类型
的 null 值,可能示意额定的信息,如:近程调用失败,异样退出。*
以上是手册的原话,所以一般来说,最好用包装类来接管数据库的数据,避免 NPE 问题。
这就是之前代码上的用 Boolean 包装类来定义而不是用根本数据类型来定义,就是这个起因。

Boolean 和 boolean 的区别就讲到这,这里是可傥,将会分享本人的所学以及所得,欢送大家一起交换。csdn 地址为:https://blog.csdn.net/kaneand…

正文完
 0