我是靠谱客的博主 英勇香水,最近开发中收集的这篇文章主要介绍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初始化变量的值,在Java中将多个变量初始化为相同的值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部