java.lang -> Boolean

java.lang -> Boolean

What is

Boolean class is a basic packed boolean type. Of type Boolean object contains a single attribute value, which is a type of boolean.
It also provides a number of the boolean converted to a String, String converted to boolean, and other methods.

Constructor

There are two constructors

public Class Boolean {
    public static final Boolean TRUE = new Boolean(true);
    
    public static final Boolean FALSE = new Boolean(false);
    
    public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
    
    private final boolean value;

    public Boolean(boolean value) {
        this.value = value;
    }

    public Boolean(String s) {
        this(parseBoolean(s));
    }
}

We can see inside maintains a boolean value, we call for an assignment in the constructor when will value.

You can also see the internal as well as two of the three constants are TRUE, FALSE Boolean values ​​representing the two states, so that we use the time can be directly used without the need to build a.

There is a TYPE received Class.getPrimitiveClass ( "boolean"); return value, we now can see into the getPrimitiveClass is a native method.

static native Class<?> getPrimitiveClass(String name);

We directly view openJDK corresponding Class.c files can be found in the corresponding method Java_java_lang_Class_getPrimitiveClass

JNIEXPORT jclass JNICALL
Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,
                                       jclass cls,
                                       jstring name)
{
    const char *utfName;
    jclass result;

    if (name == NULL) {
        JNU_ThrowNullPointerException(env, 0);
        return NULL;
    }

    utfName = (*env)->GetStringUTFChars(env, name, 0);
    if (utfName == 0)
        return NULL;

    result = JVM_FindPrimitiveClass(env, utfName);

    (*env)->ReleaseStringUTFChars(env, name, utfName);

    return result;
}

We can see the JVM to invoke JVM_FindPrimitiveClass according to "boolean" name string we pass to get to jclass, java layer was returned to Class .

And if TYPE constants in the implementation of the toString () or calls to the native method. as follows

public String toString() {
    return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
        + getName();
}

public String getName() {
    String name = this.name;
    if (name == null)
        // 会调用到 getName0();
        this.name = name = getName0();
    return name;
}

private native String getName0();

We will find that eventually will be called to getName0 () This native method. So we continue to see the C file

static JNINativeMethod methods[] = {
    {"getName0",         "()" STR,          (void *)&JVM_GetClassName},
    {"getSuperclass",    "()" CLS,          NULL},
    {"getInterfaces0",   "()[" CLS,         (void *)&JVM_GetClassInterfaces},
    {"getClassLoader0",  "()" JCL,          (void *)&JVM_GetClassLoader},
    {"isInterface",      "()Z",             (void *)&JVM_IsInterface},
    {"getSigners",       "()[" OBJ,         (void *)&JVM_GetClassSigners},
    {"setSigners",       "([" OBJ ")V",     (void *)&JVM_SetClassSigners},
    {"isArray",          "()Z",             (void *)&JVM_IsArrayClass},
    {"isPrimitive",      "()Z",             (void *)&JVM_IsPrimitiveClass},
    {"getComponentType", "()" CLS,          (void *)&JVM_GetComponentType},
    {"getModifiers",     "()I",             (void *)&JVM_GetClassModifiers},
    {"getDeclaredFields0","(Z)[" FLD,       (void *)&JVM_GetClassDeclaredFields},
    {"getDeclaredMethods0","(Z)[" MHD,      (void *)&JVM_GetClassDeclaredMethods},
    {"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors},
    {"getProtectionDomain0", "()" PD,       (void *)&JVM_GetProtectionDomain},
    {"getDeclaredClasses0",  "()[" CLS,      (void *)&JVM_GetDeclaredClasses},
    {"getDeclaringClass0",   "()" CLS,      (void *)&JVM_GetDeclaringClass},
    {"getGenericSignature0", "()" STR,      (void *)&JVM_GetClassSignature},
    {"getRawAnnotations",      "()" BA,        (void *)&JVM_GetClassAnnotations},
    {"getConstantPool",     "()" CPL,       (void *)&JVM_GetClassConstantPool},
    {"desiredAssertionStatus0","("CLS")Z",(void *)&JVM_DesiredAssertionStatus},
    {"getEnclosingMethod0", "()[" OBJ,      (void *)&JVM_GetEnclosingMethodInfo},
    {"getRawTypeAnnotations", "()" BA,      (void *)&JVM_GetClassTypeAnnotations},
};

We can see that the first line of getName0 this array will be defined in order to function JVM_GetClassName. I also remember Object article similar places do, yes yes Class category, there are ways to register a local method registerNatives corresponding.

public final class Class<T> implements java.io.Serializable,GenericDeclaration,Type,
    AnnotatedElement {

    private static native void registerNatives();
    static {
        registerNatives();
    }
}

Well, we continue to look getName0 is defined as the corresponding JVM_GetClassName function. The final function is achieved JVM_GetClassName presented below.

JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls))
  assert (cls != NULL, "illegal class");
  JVMWrapper("JVM_GetClassName");
  JvmtiVMObjectAllocEventCollector oam;
  ResourceMark rm(THREAD);
  const char* name;
  if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
    // 注意这一行函数里会去 type2name_tab 数组里去寻找
    name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls)));
  } else {
    // Consider caching interned string in Klass
    Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
    assert(k->is_klass(), "just checking");
    name = k->external_name();
  }
  oop result = StringTable::intern((char*) name, CHECK_NULL);
  return (jstring) JNIHandles::make_local(env, result);
JVM_END

We can see the eighth guild to call type2name function and, ultimately, the corresponding name based on an array, such as where the subscript 4, the name is "boolean".

const char* type2name_tab[T_CONFLICT+1] = {
    NULL, NULL, NULL, NULL,
    "boolean",
    "char",
    "float",
    "double",
    "byte",
    "short",
    "int",
    "long",
    "object",
    "array",
    "void",
    "*address*",
    "*narrowoop*",
    "*conflict*"
};

Important way

parseBoolean

public static boolean parseBoolean(String s) {
    // 判断参数 s 是否不为空并且是 "true" 字符串
    return ((s != null) && s.equalsIgnoreCase("true"));
}

public static Boolean valueOf(boolean b) {
    // 三目表达式没什么好说的
    return (b ? TRUE : FALSE);
}

public static String toString(boolean b) {
    // 三目表达式没什么好说的
    return b ? "true" : "false";
}

public String toString() {
    return value ? "true" : "false";
}

hashCode

public static int hashCode(boolean value) {
    // 即 true 返回 1231 而 false 返回 1237
    return value ? 1231 : 1237;
}

equals

public boolean equals(Object obj) {
    // 方法就是先判断是不是从 Boolean 实例化出来的,然后再继续比较是不是相等。
    if (obj instanceof Boolean) {
        return value == ((Boolean)obj).booleanValue();
    }
    return false;
}

getBoolean

public static boolean getBoolean(String name) {
    boolean result = false;
    try {
        // 从系统环境中获取指定 name 如果失败则返回 false
        result = parseBoolean(System.getProperty(name));
    } catch (IllegalArgumentException | NullPointerException e) {
    }
    return result;
}

compareTo

public int compareTo(Boolean b) {
    return compare(this.value, b.value);
}

public static int compare(boolean x, boolean y) {
    // 如果 boolean 相等则返回 0 否则 x 为真返回 1 为假返回 -1
    return (x == y) ? 0 : (x ? 1 : -1);
}

logic operation

public static boolean logicalAnd(boolean a, boolean b) {
    return a && b;
}

public static boolean logicalOr(boolean a, boolean b) {
    return a || b;
}

public static boolean logicalXor(boolean a, boolean b) {
    return a ^ b;
}

The above three methods have nothing to say we all see.

Guess you like

Origin www.cnblogs.com/liufeichn/p/11993734.html