java如何隐式创建对象?就像String类一样 [英] How java create objects implicitly? Like in case of String class

查看:137
本文介绍了java如何隐式创建对象?就像String类一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何隐式创建对象。

I can't understand how an object is created implicitly.

示例:

String s = "implicit instantiation";

我可以创建自己的类,其对象可以隐式创建吗?

Can I make my own class whose objects can be created implicitly?

推荐答案

否,字符串实例化由编译器隐式处理。只有String和Array类具有此属性。

No, String instantiation is handled implicitly by the compiler. Only the String and Array classes have this property.

String greeting = "Hello world!";
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };

Autoboxing 允许您隐式实例化原始包装类型的对象,但这也是编译器处理的特殊情况。您无法使用此功能创建自己的类。

Autoboxing allows you to implicitly instantiate objects of primitive wrapper types, but that's also a special case handled by the compiler. You can't create your own classes with this ability.

Boolean b = false;
Integer i = 0;
Double pi = 3.1416;

这篇关于java如何隐式创建对象?就像String类一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆