java 中的Boolean

来源:百度知道 编辑:UC知道 时间:2024/05/10 05:10:45
Boolean.getBoolean() 与 Boolean().booleanvalue()的区别,请举例说明。谢谢,一定有分赠。
jieely 你好!
我的程序如下:
import java.io.*;
public class StandardIn3
{
public static void main(String[] args) throws IOException
{
InputStreamReader iin=new InputStreamReader(System.in);
BufferedReader bin=new BufferedReader(iin);
boolean b;
System.out.println("输入布尔量");
b=new Boolean(bin.readLine()).booleanValue();

System.out.print( "输入的布尔量:"+b);

}}
我要将: b=new Boolean(bin.readLine()).booleanValue();
换成:b= Boolean.getBoolean(bin.readLine());
结果我在运行时:键入true 而真实结果是false

把你的程序稍稍改了一下:
public class StandardIn3 {
private static boolean b1;
private static boolean b2 = true;
public static void main(String[] args) throws IOException {
InputStreamReader iin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(iin);

System.out.println("开始的b1是: " + b1);
System.out.println("开始的b2是:" + b2);
System.out.println("输入布尔量");
b1 = new Boolean(bin.readLine()).booleanValue();
b2 = Boolean.getBoolean(bin.readLine());

System.out.println("输入的布尔量b1:" + b1);
System.out.println("输入的布尔量b2:" + b2);
}
}

运行结果:b1可以准确判断输入的boolean值;而b2为永假。我查看了一下类库也没得出结论。汗~~~你再请教一下高手吧!