Java 我们可以将 String 称为包装类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:03:38  来源:igfitidea点击:

Can we call String as a wrapper class?

javastringwrapper

提问by Reuben

Wrapper classes are used to "wrap" the primitives data types into objects so that they can be included in the activities which are reseved for the objects. String class wraps the string literals to an object.

包装类用于将原始数据类型“包装”到对象中,以便它们可以包含在为对象保留的活动中。String 类将字符串文字包装到一个对象中。

In this scenario can we call String as a wrapper class ?

在这种情况下,我们可以将 String 称为包装类吗?

采纳答案by Andreas Dolk

According to wikipedia, a (primitive) wrapper classin Java is one of those eight classes that wrap a (=one) primitive value. Stringwrapsa char[]so according to this it is nota (primitive) wrapper class.

根据维基百科,Java 中的(原始)包装类是包装(=一个)原始值的八个类之一。String缠绕char[]所以根据这一点,是不是一个(原语)包装类。

Furthermore, Stringis not designed to wrapor decoratea character array. Stringhas been designed to model a string, a character sequence and the current implementation uses an internal char[]. But Sun could also have chosen to use a byte[]a long with a character encoding hint, a collection or something native. That doesn't matter.

此外,String不是为了包装装饰字符数组而设计的。String已设计为对字符串、字符序列建模,当前实现使用内部char[]. 但是 Sun 也可以选择使用byte[]带有字符编码提示、集合或本机内容的 long 。那没关系。

That's different for the primitive wrappers: they have been designed onlyto wrap a primitive, to adapta java primitive to java.lang.Object.

这对于原始包装器来说是不同的:它们被设计为用于包装原始数据,将Java 原始数据适配java.lang.Object.

回答by Suraj Chandran

No. String is not a wrapper class, simply because there is no parallel primitive type that it wraps.

不。 String 不是包装类,仅仅是因为它没有包装的并行原始类型。

From wiki:

来自维基:

A primitive wrapper class in the Java programming language is one of eight classes provided in the java.lang package to provide object methods for the eight primitive types

Java 编程语言中的原始包装类是 java.lang 包中提供的八个类之一,为八种原始类型提供对象方法

回答by Deepak Bala

A string is a representation of a char sequence but not necessarily a 'wrapper'. Autoboxing and unboxing for example do not apply to String. But they do apply to primitives such as int long etc.

字符串是字符序列的表示,但不一定是“包装器”。例如,自动装箱和拆箱不适用于字符串。但它们确实适用于 int long 等原语。

回答by James Scriven

Conventionally no since it doesn't wrap one of the eight primitives, but I don't think it would hurt to call it this.

通常不会,因为它没有包装八个原语之一,但我认为这样称呼它不会有什么坏处。

回答by Kwebble

No, a String doesn't wrap the literal value. A literal gives you a different way to create a String object. But the literal itself is an object, so you could do things like:

不,字符串不会包装文字值。文字为您提供了一种创建 String 对象的不同方式。但是文字本身是一个对象,因此您可以执行以下操作:

System.out.println("Hello world".toUpperCase());

Not that you would want to do this.

并不是说你会想要这样做。

回答by surya

No we cannot call string as a wrapper class.Because sun people not included string in wrapper classes.

不,我们不能将字符串称为包装类。因为太阳人们没有在包装类中包含字符串。