【问题标题】:What does it mean when a value in variable declaration is in parenthesisWhat does it mean when a value in variable declaration is in parenthesis
【发布时间】:2022-12-02 01:09:01
【问题描述】:

What is the difference in writing a variable declaration with a value like this

String name = ("name");
int age = (42);
String city = ("city");`

and this

String name = "name";
int age = 42;
String city = "city";`

Does it have any effect on memory?

I have tried both, no problem the code still works, but don't know what the difference is.

【问题讨论】:

  • the () is useless
  • One version contains a completely useless set of braces, the other version doesn't.
  • No difference. It's like using an umbrella even if it's not raining.
  • If you are wondering when it will make sense to use braces. String name = "name" + 1 + 3; will result in name13 while String name2 = "name" + (1 + 3); for making sure the addition is done before the string concatenation will result in name4

标签: java


【解决方案1】:

There's no relevant difference. You simply put the value into parenthesis which only group operations together, but since no operations happen inside the parenthesis, they do nothing.

If you want the formal definition (which uses quite a few more words to say essentially the same thing) see this chapter of the JLS.

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 1970-01-01
    • 2022-12-27
    • 2022-12-27
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多