将字符串放在括号中的语法意义
我喜欢让自己与语法上的事情混淆,而不是专注于真正做的事情。 :)
我知道我能用这个东西做什么,但我仍然想知道幕后到底发生了什么。
所以...我只是想知道将字符串放在括号内有意义的原因和情况是什么。我看到一个返回类似内容的方法
return (string1 + " " + string2);
I like to confuse myself with syntactical things and not to concentrate on really doing stuff. :)
I know what I can do with this thing but I still want to know what is really happening under the hood.
So... I just wonder what are the reasons and situations where putting strings inside parentheses is meaningful. I saw a method that returned something like
return (string1 + " " + string2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
括号在此示例中不起作用。您发布的代码与此等效:
一般来说,括号和
String
或字符串文字之间没有特定的交互。括号与“正常”表达式具有相同的语义:对操作进行分组。在某些情况下,可能需要括号来指定正确的解释:
The parenthesis have no effect in this example. The code you posted is equivalent to this:
Generally speaking there is no specific interaction between parenthesis and
String
s or string literals. Parenthesis have the same semantics as in "normal" expressions: to group operations.In some cases parenthesis might be necessary to specify the correct interpretation:
它是一个合法的构造,与代数中的括号具有相同的目的,以确保操作在返回之前完成。实际上,在这种情况下,不需要它,如果您这样做,
结果将是相同的。也许代码的作者认为它可能更具可读性?
It is a legal construct and it has the same purpose as parenthesis in algebra, to ensure that the operation gets done before the return. In actuality in this case it is not needed and if you did
The result would be the same. Maybe the author of the code thought it might be more readable ?
只是可读性。如果你想强调返回的对象,你可以加上括号。但它们不是必要的。
Just readability. If you want to emphasis on the returning object, you put the parentheses. But they are not necessary.
这使得
return
看起来像一个函数调用,但实际上不是。因此,这是一个不应使用括号的示例。This makes
return
look like a function call, which it in fact is not. So, this is a example where you should not use parentheses.这是另一个例子:
和:
是两种截然不同的野兽。
Here's another example:
and:
are two very different beasts.