Best approach to converting Boolean object to string in java

后端 未结 7 861
暖寄归人
暖寄归人 2020-12-13 01:51

I am trying to convert boolean to string type...

Boolean b = true;
String str = String.valueOf(b);

or

Boolean b = true;
Str         


        
相关标签:
7条回答
  • 2020-12-13 02:39

    If you see implementation of both the method, they look same.

    String.valueOf(b)

    public static String valueOf(boolean b) {
            return b ? "true" : "false";
        }
    

    Boolean.toString(b)

    public static String toString(boolean b) {
            return b ? "true" : "false";
        }
    

    So both the methods are equally efficient.

    0 讨论(0)
提交回复
热议问题