Best performance for string to boolean conversion

后端 未结 3 1893
青春惊慌失措
青春惊慌失措 2021-01-19 13:23

Which has best performance while converting String to boolean from the below options.

  1. boolean value = new Boolean(\"true\").booleanValue();
  2. <
3条回答
  •  深忆病人
    2021-01-19 13:56

    Here is the source, looks like Tomasz beat me to answering the why:

    public static Boolean valueOf(String s) {
    return toBoolean(s) ? TRUE : FALSE;
    }
    public static boolean parseBoolean(String s) {
        return toBoolean(s);
    }
    public Boolean(String s) {
    this(toBoolean(s));
    }
    private static boolean toBoolean(String name) { 
    return ((name != null) && name.equalsIgnoreCase("true"));
    }
    

提交回复
热议问题