Can I write a literal initializer in Java for a non-primitive type?

我的未来我决定 提交于 2019-12-02 04:18:39

String literals as a special case in Java. Numbers don't really have literals - what you're seeing here is a primitive (such as 2) being autoboxed to its wrapper class (such as Integer). Since all the numeric wrappers extends Number, this assignments works (in the same way that Object o = 4; would be a legal statement).

You can not add your own literals to the language, though. The best you could do is to create some static function that produces a new instance of your class.

No, this is not possible. There is no way whatsoever to initialize a value other than a primitive or a String like you want.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!