【发布时间】: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 inname13whileString name2 = "name" + (1 + 3);for making sure the addition is done before the string concatenation will result inname4
标签: java