java boolean 包_java Boolean包装类工作笔记

本文章为各位介绍一篇关于java Boolean包装类工作笔记,希望这篇文章能够帮助到各位同学,有兴趣的朋友可以进来看看吧。

题目:问下列代码运行结果是什么?

 代码如下复制代码

package quiz;

public class Quiz22 {

static String str;

public static boolean test1(){

return new Boolean("1");

}

public static boolean test2(){

return new Boolean(str);

}

public static void main(String[] args) {

if(test1())

System.out.print("1");

if(!test2())

System.out.print("2");

if(test1() != test2())

System.out.print("3");

}

}

输出:

2

先看下Boolean类的源代码:(部分省略了,只保留了关键代码)

 代码如下复制代码

public final class Boolean implements java.io.Serializable, Comparable

{

private final boolean value;

public Boolean(boolean value) {

this.value = value;

}

public Boolean(String s) {

this(toBoolean(s));

}

public static int compare(boolean x, boolean y) {

return (x == y) ? 0 : (x ? 1 : -1);

}

private static boolean toBoolean(String name) {

return ((name != null) && name.equalsIgnoreCase("true"));

}

public boolean equals(Object obj) {

if (obj instanceof Boolean) {

return value == ((Boolean)obj).booleanValue();

}

return false;

}

public int compareTo(Boolean b) {

return compare(this.value, b.value);

}

public static boolean parseBoolean(String s) {

return toBoolean(s);

}

public static Boolean valueOf(boolean b) {

return (b ? TRUE : FALSE);

}

/**

* Returns a {@code Boolean} with a value represented by the

* specified string.  The {@code Boolean} returned represents a

* true value if the string argument is not {@code null}

* and is equal, ignoring case, to the string {@code "true"}.

*

* @param   s   a string.

* @return  the {@code Boolean} value represented by the string.

*/

public static Boolean valueOf(String s) {

return toBoolean(s) ? TRUE : FALSE;

}

public String toString() {

return value ? "true" : "false";

}

/**

* Returns a hash code for this {@code Boolean} object.

*

* @return  the integer {@code 1231} if this object represents

* {@code true}; returns the integer {@code 1237} if this

* object represents {@code false}.

*/

public int hashCode() {

return value ? 1231 : 1237;

}

}

从上面可以看出:

1.Boolean支持使用String或boolean来创建对象。

如果是用boolean构建的,则Boolean对象自动拆箱后还是原来的值。

如果是用string来构建对象,则如果该String为null或string.equalsIgnoreCase("true")返回false,则Boolean拆箱后的值就为false。

2.Boolean类实现了Comparable接口,即实现了ComparaTo方法

分析题目:

test1()  等价于 (new Boolean("1")) 自动拆箱 ,则为 false

!test2()等价于  ! (new Boolean(str)   自动拆箱)    ,因为str为null,所以结果true。

test1() != test2()    ,结果为 false

如果test1和test2方法的返回值不是boolean,而是Boolean对象,则结果就不一样了。因为是new两个对象,地址肯定不同,而且Boolean代码中不想Integer有cache。

 代码如下复制代码

public class DecoratorPatternEx {

public static void main(String[] args) {

Boolean a =   new Boolean("1");

Boolean b =   new Boolean("1");

Boolean  a1 = new Boolean(true);

Boolean b1 = new Boolean(true);

if ( a == b){

System.out.println( true);

}else {

System.out.println(false);

}

if ( a1 == b1){

System.out.println( true);

}else {

System.out.println(false);

}

}

}

结果:

false

false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 MyBatis 中,当使用 `select` 语句查询结果为 `Boolean` 类型时,会出现以下两种情况: 1. 查询结果为 0 或者为 null 时,返回的是 `false`; 2. 查询结果不为 0 且不为 null 时,返回的是 `true`。 如果你需要自定义返回结果的逻辑,可以通过实现 `org.apache.ibatis.type.TypeHandler` 接口来完成。下面是一个示例代码: ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BooleanTypeHandler extends BaseTypeHandler<Boolean> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException { ps.setInt(i, parameter ? 1 : 0); } @Override public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException { int result = rs.getInt(columnName); return result == 1; } @Override public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException { int result = rs.getInt(columnIndex); return result == 1; } @Override public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { int result = cs.getInt(columnIndex); return result == 1; } } ``` 在 `setNonNullParameter` 方法中,将 Java 类型的 `Boolean` 转换成数据库类型的 `int`,并设置到 `PreparedStatement` 对象中。 在 `getNullableResult` 方法中,从 `ResultSet` 或者 `CallableStatement` 对象中获取 `int` 类型的结果,并将其转换成 `Boolean` 类型。如果结果为 1,则返回 `true`,否则返回 `false`。 最后,将该类型处理器注册到 MyBatis 的配置文件中即可: ```xml <typeHandlers> <typeHandler javaType="java.lang.Boolean" handler="com.example.BooleanTypeHandler"/> </typeHandlers> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值