我是靠谱客的博主 英勇香水,这篇文章主要介绍java初始化变量的值,在Java中将多个变量初始化为相同的值,现在分享给大家,希望可以做个参考。

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初始化变量内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部