Skip to main content

JVM源码解读-Boolean类

finen...About 3 minJava-SourceJava-Source

1.类图

Boolean-Class
Boolean-Class

2.存储位置

  • Boolean类中有两个状态码,TRUE和FALSE,这两个属性存在JVM方法区的静态区,对应源码如下:
    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code true}.
     */
    public static final Boolean TRUE = new Boolean(true);

    /**
     * The {@code Boolean} object corresponding to the primitive
     * value {@code false}.
     */
    public static final Boolean FALSE = new Boolean(false);
  • 通过new Boolean()创建的对象都存储在堆区。

3.源码解读

3.1 核心方法

Boolean构造方法

  • 从Java9开始弃用
public final class Boolean implements java.io.Serializable,
        Comparable<Boolean> {
    // 将传过来的boolean作为参数
    public Boolean(boolean value) {
        this.value = value;
    }

    // 将输入拖来的字符串解析为true或者false
    public Boolean(String s) {
        this(parseBoolean(s));
    }
}
public class BooleanDemoTest {
    @Test
    public void testBoolean() {
        Boolean b1 = new Boolean("TRue"); // true
        Boolean b2 = new Boolean("True"); // true

        Boolean b3 = new Boolean("fAlse"); // false
        Boolean b4 = new Boolean("False"); // false
        Boolean b5 = new Boolean("Trrue"); // false
    }
}

valueOf方法

public final class Boolean implements java.io.Serializable,
        Comparable<Boolean> {
    // 根据值返回TRUE或者FALSE,对应Boolean的两个属性
    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }
    public static Boolean valueOf(String s) {
        return parseBoolean(s) ? TRUE : FALSE;
    }
}

parseBoolean方法

该方法是将字符串值转为对应boolean值,如果传入的值为true或者false的大小写,则返回true或者false,如果传入不是true或者false(null或者其他),则返回false

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public static boolean parseBoolean(String s) {
        return ((s != null) && s.equalsIgnoreCase("true"));
    }
}
public class BooleanDemoTest {
    @Test
    public void testParseBoolean() {
        System.out.println(Boolean.parseBoolean(null));    // false
        System.out.println(Boolean.parseBoolean("True"));  // true
        System.out.println(Boolean.parseBoolean("False")); // false
        System.out.println(Boolean.parseBoolean("ttur"));  // false
    }
}

booleanValue方法

将对应的Boolean值转为对应的boolean值。

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public boolean booleanValue() {
        return value;
    }
}

toString方法

该方法是将boolean值转为对应的boolean字符串

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    // 根据不同的boolean值返回对应boolean字符串
    public static String toString(boolean b) {
        return b ? "true" : "false";
    }
    public String toString() {
        return value ? "true" : "false";
    }
}

getBoolean方法

该方法是获取系统设置的参数,并且将对应的参数转为true或者false,如果没有获取到参数或者获取错误都返回false。

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public static boolean getBoolean(String name) {
        boolean result = false;
        try {
            result = parseBoolean(System.getProperty(name));
        } catch (IllegalArgumentException | NullPointerException e) {
        }
        return result;
    }
}
public class BooleanDemoTest {
    @Test
    public void testGetBoolean() {
        System.setProperty("test-boolean", "55");
        System.out.println(Boolean.getBoolean("test-boolean")); // false
        System.setProperty("test-boolean2", "true");
        System.out.println(Boolean.getBoolean("test-boolean2")); // true
    }
}

compare方法

该方法是比较两个boolean值,并且返回对应的int值,如果两个值相等则返回0,如果两个值不等,则返回1或者-1,如果第一个值是false则返回-1,如果第一个值为true则返回1

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public static int compare(boolean x, boolean y) {
        return (x == y) ? 0 : (x ? 1 : -1);
    }
}
public class BooleanDemoTest {
    @Test
    public void testCompare() {
        System.out.println(Boolean.compare(true, true)); // 0
        System.out.println(Boolean.compare(true, false)); // 1
        System.out.println(Boolean.compare(false, true)); // -1
        System.out.println(Boolean.compare(false, false)); // 0
    }
}

logicalAnd方法

逻辑和方法,该方法要求两个值必须同时满足才返回true,否则返回false

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public static boolean logicalAnd(boolean a, boolean b) {
        return a && b;
    }
}
public class BooleanDemoTest {
    @Test
    public void testLogicalAnd() {
        System.out.println(Boolean.logicalAnd(true, false)); // false
        System.out.println(Boolean.logicalAnd(true, true)); // true
        System.out.println(Boolean.logicalAnd(false, false)); // false
    }
}

logicalOr方法

逻辑或方法,传入的两个boolean值只要有一个为true则返回true,否则返回false

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public static boolean logicalOr(boolean a, boolean b) {
        return a || b;
    }
}
public class BooleanDemoTest {
    @Test
    public void testLogicalOr() {
        System.out.println(Boolean.logicalOr(true, false)); // true
        System.out.println(Boolean.logicalOr(false, false)); // false
    }
}

logicalXor方法

该方法返回对分配的布尔操作数执行逻辑异或运算的结果。

public final class Boolean implements java.io.Serializable,
Comparable<Boolean> {
    public static boolean logicalXor(boolean a, boolean b) {
        return a ^ b;
    }
}
public class BooleanDemoTest {
    @Test
    public void testLogicalXor() {
        System.out.println(Boolean.logicalXor(true, false));  // true
        System.out.println(Boolean.logicalXor(false, false)); // false
        System.out.println(Boolean.logicalXor(false, true));  // true
        System.out.println(Boolean.logicalXor(true, true));   // false
    }
}

4.使用建议

  • 推荐使用Boolean.TRUE或Boolean.valueOf(true)或Boolean.valueOf("true"),避免重复创建对象
  • 两个值进行比较时,使用Boolean.TRUE.equals(xxx)进行判断
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8