概述
I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have:
String one = "", two = "", three = "" etc...
But I'm looking for something like:
String one,two,three = ""
Is this something that is possible to do in java? Keeping efficiency in mind.
解决方案
String one, two, three;
one = two = three = "";
This should work with immutable objects. It doesn't make any sense for mutable objects for example:
Person firstPerson, secondPerson, thirdPerson;
firstPerson = secondPerson = thirdPerson = new Person();
All the variables would be pointing to the same instance. Probably what you would need in that case is:
Person firstPerson = new Person();
Person secondPerson = new Person();
Person thirdPerson = new Person();
Or better yet use an array or a Collection.
最后
以上就是英勇香水为你收集整理的java初始化变量的值,在Java中将多个变量初始化为相同的值的全部内容,希望文章能够帮你解决java初始化变量的值,在Java中将多个变量初始化为相同的值所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复