primitive-boolean To String concatenation/conversion

13,188

Solution 1

The exact rules are spelled out in the Java Language Specification, §5.1.11. String Conversion

According to those rules, "str" + bool is equivalent to:

"str" + new Boolean(bool).toString()

That said, the compiler is permitted considerable leeway in how exactly the overall expression is evaluated. From JLS §15.18.1. String Concatenation Operator +:

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

For example, with my compiler the following:

boolean bool = true;
System.out.println("the value of bool is : " + bool);

is exactly equivalent to:

boolean bool = true;
System.out.println(new StringBuilder("the value of bool is : ").append(bool).toString());

They result in identical bytecodes:

Code:
   0: iconst_1      
   1: istore_1      
   2: getstatic     #59                 // Field java/lang/System.out:Ljava/io/PrintStream;
   5: new           #166                // class java/lang/StringBuilder
   8: dup           
   9: ldc           #168                // String the value of bool is : 
  11: invokespecial #170                // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
  14: iload_1       
  15: invokevirtual #172                // Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
  18: invokevirtual #176                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
  21: invokevirtual #69                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  24: return        

Solution 2

It's a compiler thing. If the right operand for concatenation is an object, the object is sent the toString() method whereas if the operand is a primitive then the compiler knows which type-specific behavior to use to convert the primitive to an String.

Solution 3

The compiler translates it to

StringBuilder sb = new StringBuilder("the value of bool is : ");
sb.append(true);
System.out.println(sb.toString());

The rules of concatenation and conversion are explained in the JLS.

Share:
13,188
Vinay W
Author by

Vinay W

Android Expert. Python enthusiast. Have an eye for UI/UX. Relentlessly curious.

Updated on June 04, 2022

Comments

  • Vinay W
    Vinay W almost 2 years

    how does this work? I can't seem to find an answer.

    boolean bool=true;
    System.out.println("the value of bool is : " + true);
    //or
    System.out.println("the value of bool is : " + bool);
    
    • What are the things that are going on behind the scene?
    • how does the boolean gets casted to the String as a boolean cannot be implicitly type casted?
    • Is Autoboxing/Unboxing involved?
    • Are methods like toString() or String.valueOf() are involved in some way?
  • Vinay W
    Vinay W over 11 years
    i see.. can you point me to a documentation which states that?
  • JB Nizet
    JB Nizet over 11 years
    @VinayWadhwa: see my answer for a reference.
  • Vinay W
    Vinay W over 11 years
    does the fact that both code snippets create the same bytecode necessarily mean that the former IS converted to the latter? (before being converted to the bytecode, that is)
  • Vinay W
    Vinay W over 11 years
    which compiler? every compiler? if you could point me to the source of of this info, it would be greatly appreciated. The reference you provided does not relate to your answer, i think..
  • JB Nizet
    JB Nizet over 11 years
    It says: An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression. To know the rules, read the JLS. To know how it actually works, read the bytecode generated by your compiler.
  • Montre
    Montre over 11 years
    @VinayWadhwa I don't think the JLS specifies how you should implement string concatenation – merely what the outcome should be. However, as far as I can remember, the Snorcle Java compiler has implemented it using a StringBuffer, then a StringBuilder. It's probably entirely permissible to have string concatenation be an intrinsic operation in your JVM.
  • Montre
    Montre over 11 years
    @VinayWadhwa This seems to be implied by §15.18.1: "To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique…" and "For primitive types, an implementation may also optimize away the creation of a wrapper object…"