java的boolean与string类型转换

本文介绍Java中如何使用Boolean.valueOf方法将字符串转换为布尔值。该方法仅当传入的字符串为非空且忽略大小写等于true时返回true,其他情况一律视为false。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Boolean.valueOf(“TRUE”)==true//此处的参数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;
}

private static boolean toBoolean(String name) {
    return ((name != null) && name.equalsIgnoreCase("true"));
}

熟悉C语言的人都知道,C里面0是false,其余的非零数值都是true
java里面也有一个方法用来实现从string类型转到boolean类型,但是更加简单粗暴,
Boolean.valueOf(“true”)//此处的参数true忽略大小写
其他的值都是false,详见上面源码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值