[英]Operator in print statement
Example:例子:
System.out.println("the investment doubled after "+year+" years.");
Can someone please explain why the int
variable year
is inside quotations and pluses?有人可以解释为什么
int
变量year
在引号和加号中吗?
Adding some spacing around the operators may make this statement clearer:在运算符周围添加一些间距可能会使此语句更清晰:
System.out.println("the investment doubled after " + year + " years.");
This statement prints the result of a concatination of three strings, achieved by the two +
operators:此语句打印由两个
+
运算符实现的三个字符串的连接结果:
"the investment doubled after "
int
variable year
to a string int
变量year
隐式转换为字符串" years."
It is like this.它是这样的。 And
+
is used for String
concatenation.而
+
用于String
连接。 In this year
is converted into String
from int
implicitly.在这
year
中从int
隐式转换为String
。
"the investment doubled after " + year + " years."
^-------inside quotes-------^ ^-----^Inside quotes
Here you are using the public void println(String x);这里你使用的是 public void println(String x); method.
方法。 Which means the integer will concatenated to the string when you append the years to the string, that is the reason we are using the + operator.
这意味着当您将年份附加到字符串时,整数将连接到字符串,这就是我们使用 + 运算符的原因。
Your variable year is not inside quotes, it's actually outside of quotes.您的变量 year 不在引号内,它实际上在引号之外。 It is in between of + sign because you are concatenating it.
它介于 + 符号之间,因为您正在连接它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.