java 原始布尔到字符串连接/转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13894892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:21:20  来源:igfitidea点击:

primitive-boolean To String concatenation/conversion

javastringcastingbooleanstring-concatenation

提问by Vinay W

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/Unboxinginvolved?
  • Are methods like toString()or String.valueOf()are involved in some way?
  • 幕后发生的事情是什么?
  • 布尔值如何转换为字符串,因为布尔值不能隐式转换?
  • 是否涉及自动装箱/拆箱
  • 方法是否像toString()String.valueOf()以某种方式参与?

回答by NPE

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

Java 语言规范第5.1.11 节详细说明了确切的规则。字符串转换

According to those rules, "str" + boolis equivalent to:

根据这些规则,"str" + bool相当于:

"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 +:

也就是说,编译器在如何准确评估整个表达式方面被允许有相当大的回旋余地。来自 JLS §15.18.1。字符串连接运算符 +

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 StringBufferclass or a similar technique to reduce the number of intermediate Stringobjects 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.

实现可以选择在一个步骤中执行转换和连接,以避免创建然后丢弃中间 String 对象。为了提高重复字符串连接的性能,Java 编译器可以使用StringBuffer类或类似技术来减少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        

回答by Chris Gerken

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.

这是编译器的事情。如果连接的正确操作数是对象,则向对象发送toString()方法,而如果操作数是原语,则编译器知道使用哪种特定于类型的行为将原语转换为字符串。

回答by JB Nizet

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.

JLS中解释了连接和转换的规则。