为什么打印"" + 变量会将每个变量单独输出而没有间距?

huangapple go评论75阅读模式
英文:

Why printing "" + variables output each variable individually without spacing?

问题

以下是翻译好的部分:

我的一个作业中有这段代码:

System.out.println("" + x + y + count);

这段代码会将变量 x、y 和 count 的值分别输出,没有任何空格。我想在网上了解更多相关信息。然而,我似乎找不到正确的关键词来在线搜索。有人可以解释一下其中的逻辑,或者给我指出一个名称或关键词来描述这种情况吗?

我一直以来都把 "" 视为输出字符串的工具,所以对于这段代码感到困惑。

提前感谢您的帮助。

英文:

One of my assignments has this code:

    System.out.println("" + x + y + count);

that outputs the value of x, y and count individually without any spaces. I would like to know more about it online. However, I can't seem to find the right keywords to search it up online. Can someone please explain to me the logic behind this or perhaps point to me a name or keyword for such a situation?

I have always known the " " as a tool to print out a string so I'm confused by this.

Thanks in advance.

答案1

得分: 1

或者您可以像这样使用字符串格式:

System.out.printf("%d%d%d", x, y, count);
英文:

Or you can use String format like this:

System.out.printf("%d%d%d", x, y, count);

答案2

得分: 1

如果我们应用标准的Java优先规则,语句:

System.out.println("" + x + y + count);

等同于

System.out.println((("" + x) + y) + count);

然后我们来看一下 + 的含义

  • 如果 ab 的静态类型都是数字类型(原始数字类型或它们的包装类型),那么 a + b 就是数字相加。

  • 否则,a + b 就是字符串连接。两个参数被转换为字符串,然后连接成字符串。

基于这个,我们可以说示例中所有的 + 运算符都会被看作是字符串连接。


如果你希望在 xycount 之间有空格,你需要添加一些字符串字面量;例如:

System.out.println("" + x + " " + y + " " + count);

或者,更简单地:

System.out.println(x + " " + y + " " + count);

如果你希望 xycount 被相加(假设它们是数值类型),你可以这样写:

System.out.println("" + (x + y + count));

或者,更简单地:

System.out.println(x + y + count);

后者使用了 println 的另一个重载。


> 我一直将 "" 视为一个输出字符串的工具,所以我对此感到困惑。

嗯... 实际上它是一个空字符串字面量。使用 "" + x 的方式只是一种将 x 转换为 String 的习惯用法。空字符串字面量还有其他用途。主要用途是表示一个长度为零的字符串。

英文:

If we apply standard Java precedence rules, the statement:

System.out.println("" + x + y + count);

is equivalent to

System.out.println((("" + x) + y) + count);

Then we look at the meaning of +

  • If the static types of both a and b are numeric types (either primitive numeric or their boxed types) then a + b is numeric addition.

  • Otherwise, a + b is string concatenation. The two arguments are converted to strings and the strings are concatenated.

Based on this we can say that all of the + operators in the example will be treated as string concatenations.


If you want spaces between x, y and count you need to add some string literals; e.g.

System.out.println("" + x + " " + y + " " + count);

or, more simply:

System.out.println(x + " " + y + " " + count);

If you wanted x, y and count to be added (assuming that they are numeric), then you could write this:

System.out.println("" + (x + y + count));

or, more simply:

System.out.println(x + y + count);

The latter is using a different overload of println.


> I have always known the "" as a tool to print out a string so I'm confused by this.

Ummm ... it is actually an empty string literal. The usage "" + x is simply an idiom for converting x to a String. The empty string literal has other uses too. The main one is to represent a String with zero characters.

答案3

得分: 0

`""` 是一个空字符串。在Java中,当你将一个字符串与其他可以转换为字符串的原始类型连接在一起时,结果就是一个字符串。这意味着以下代码:

    System.out.println("" + something);

是一种不同的写法:

    System.out.println(String.valueOf(something));

然而,在你的情况下,`"" + x + y + count` 表示元素被转换为字符串,然后进行连接 - 这意味着如果 `x==1`,`y==2`,`count==3`,结果将会是 `123`。如果你想要将结果转换为字符串,你需要在转换为字符串之前表明应该先进行计算,例如使用括号:

    System.out.println("" + (x+y+count));

这种情况下的输出将会是 `6`。
英文:

The "" is an empty String. In Java when you concatenate a String with other primitives that can be cast to String the result is a String. That means that the code

System.out.println("" + something);

is a different way to write

System.out.println(String.valueOf(something));

However in your scenario the "" + x + y + count means that the elements are converted to String and then concatenated - this means that if x==1, y==2, count==3 the result would be 123. If you wanted to just cast the result to String you would have to indicate that the computation should happen before casting to String for example by using brackets

System.out.println("" + (x+y+count));

The output of this would be 6.

huangapple
  • 本文由 发表于 2020年9月28日 16:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64098439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定