Can we call String as a wrapper class?
Asked Answered
L

6

11

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.

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

Lh answered 6/7, 2011 at 9:42 Comment(2)
I like to call String "Fred". No, I wouldn't call that a wrapper class. I don't know what it improves if I do.Cacology
wrapper class implicitly refers to a PRIMITIVE wrapper class. so considering it's implicit definition, String is not a wrapper class.Lontson
G
11

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

Furthermore, String is not designed to wrap or decorate a character array. String has 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.

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

Goulette answered 6/7, 2011 at 9:45 Comment(0)
K
9

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

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

Kwangchow answered 6/7, 2011 at 9:45 Comment(0)
B
2

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.

Betseybetsy answered 6/7, 2011 at 9:52 Comment(0)
F
1

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:

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

Not that you would want to do this.

Faradmeter answered 6/7, 2011 at 10:1 Comment(0)
A
0

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

Apostles answered 6/7, 2011 at 9:53 Comment(0)
L
-3

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

Likeness answered 27/12, 2013 at 11:36 Comment(2)
Is that answer useful? seeing all the other (and one accepted)Scraggy
Haha funny but not answers the above questionParkins

© 2022 - 2024 — McMap. All rights reserved.