Primitive wrapper class

Primitive wrapper class

A primitive wrapper class in the Java programming language is one of eight classes provided in the Javadoc:SE|package=java. _ja. package] to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable. J2SE 5.0 introduced autoboxing of primitive types into their wrapper object, and automatic unboxing of the wrapper objects into their primitive value—the implicit conversion between the wrapper objects and primitive values.

Wrapper classes are used to represent primitive values when an Javadoc:SE|java/lang|Object is required. The wrapper classes are used extensively with Javadoc:SE|java/util|Collection classes in the Javadoc:SE|package=java.util|java/util package and with the classes in the Javadoc:SE|package=java.lang.reflect|java/lang/reflect reflection package.

The primitive wrapper classes and their corresponding primitive types are:

:

The Byte, Short, Integer, Long, Float, and Double wrapper classes are all subclasses of the Javadoc:SE|java/lang|Number class.

Use of primitive wrappers in reflection

When looking for methods with a particular signature, each argument of the method signature must be defined using its corresponding Class type. To be able to define primitive arguments, each primitive type is associated with an immutable Class that represent this type when using reflection.

Suppose a class "Foo" with one method called "doSomething", having only one primitive "int" parameter. If we want to call this method using reflection, we will have to use the Type associated with the "int" primitive: // Without reflectionFoo foo = new Foo();foo.doSomething(1);

// With reflectionClass clazz = Class.forName("Foo");Object foo = clazz.newInstance();Method method = clazz.getMethod("doSomething", Integer.Type);method.invoke(foo, 1);

Void

Although it is not a wrapper class, the Javadoc:SE|java/lang|Void class is similar in that it provides an object representation of the void return type. The Void class is an uninstantiable placeholder class used by the Javadoc:SE|package=java.lang.reflect|java/lang/reflect API to hold a reference to the Javadoc:SE|java/lang|Class object representing the Java keyword void.

Atomic wrapper classes

With Java 5.0, additional wrapper classes were introduced in the Javadoc:SE|package=java.util.concurrent.atomic|java/util/concurrent/atomic package. These classes are mutable and cannot be used as a replacement for the regular wrapper classes. Instead, they provide atomic operations for addition, increment and assignment.

The atomic wrapper classes and their corresponding types are:

:

The AtomicInteger and AtomicLong classes are subclasses of the Number class. The AtomicReference class accepts the type parameter V that specifies the type of the object reference. (See "Generics in Java" for a description of type parameters in Java).

See also

* Java Platform, Standard Edition#java. _ja. java.lang.reflect
* Java programming language
* Java syntax


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Wrapper — The term wrapper generally refers to a type of packaging, such as a flat sheet made out of paper, cellophane, or plastic to enclose an object. In computing, it may also refer to: * Driver wrapper, software that functions as an adapter between an… …   Wikipedia

  • Primitive type — In computer science, primitive types as distinct from composite types are data types provided by a programming language as basic building blocks. Primitive types are also known as built in types or basic types .Depending on the language and its… …   Wikipedia

  • Comparison of Java and C Sharp — This is a comparison of the C# programming language with the Java programming language. As the two are both garbage collected runtime compiled languages with syntax derived from C and C++, there are many similarities between Java and C#. However …   Wikipedia

  • Object type — In computer science, an object type (a.k.a. wrapping object) is a datatype which is used in object oriented programming to wrap a non object type to make it look like a dynamic object.Some object oriented programming languages make a distinction… …   Wikipedia

  • Java Platform, Standard Edition — or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use.In practical terms, Java SE consists of a virtual machine, which must be used to run Java… …   Wikipedia

  • Value type — In object oriented programming, a value type is a data type that can exist outside dynamic memory allocation. Unlike reference types, value types can be directly embedded into composite objects.Primitive types are always value types.Value types… …   Wikipedia

  • Java version history — The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community Process (JCP) …   Wikipedia

  • Comparison of C Sharp and Java — The correct title of this article is Comparison of C# and Java. The substitution or omission of the # sign is because of technical restrictions. Programming language comparisons General comparison Basic syntax Basic instructions …   Wikipedia

  • Adapter (Entwurfsmuster) — Der Adapter – auch die Hüllenklasse[1] oder der Wrapper (vom englischen wrapper für „Verpackung“ oder „Umschlag“) genannt – ist ein Entwurfsmuster aus dem Bereich der Softwareentwicklung und gehört zu der Kategorie der Strukturmuster (englisch… …   Deutsch Wikipedia

  • Adapter Design Pattern — Der Adapter (englisch Adapter, Wrapper) ist ein Entwurfsmuster aus dem Bereich der Softwareentwicklung und gehört zu der Kategorie der Strukturmuster (Structural Patterns). Das Muster dient zur Übersetzung einer Schnittstelle in eine andere.… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”