avatar
a stupid question# Java - 爪哇娇娃
p*p
1
which is better
String add;
while(XXX) {
add=XXXX
....
}
or
while(XXX) {
String add=XXXX
....
}
avatar
g*y
2
I would use second -- it restrict the variable "add" in local, that gives
yourself a better protection from making mistakes.
Example ->
String add;
while(XXX) {
add = XXXX
... (200 lines)
}
String message = getMessage(add);
This program runs fine. One month later, you accidentally change that "add"
in the 200 lines, forgot that message also depends on "add". Now your
trouble starts.
During a variable's life cycle, you always need to remeber and be
responsible for it. Just to make your life

【在 p***p 的大作中提到】
: which is better
: String add;
: while(XXX) {
: add=XXXX
: ....
: }
: or
: while(XXX) {
: String add=XXXX
: ....

avatar
p*p
3
why no diff in performance? new instances are repeatly created
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。