Search: in
Primitive wrapper class
Primitive wrapper class Encyclopedia
  Tutorials     Encyclopedia     Dictionary     Directory  
Primitive_wrapper_class Email this to a friend      Primitive_wrapper_class

Primitive wrapper class

A primitive wrapper class in the Java programming language is one of eight classes provided in the 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 is required. The wrapper classes are used extensively with classes in the package and with the classes in the reflection package.

The primitive wrapper classes and their corresponding primitive types are:

! Primitive type !! Wrapper class !! Constructor Arguments |- | byte || || byte or String |- | short || || short or String |- | int || || int or String |- | long || || long or String |- | float || || float, double or String |- | double || || double or String |- | char || || char |- | boolean || || boolean or String |}

The Byte, Short, Integer, Long, Float, and Double wrapper classes are all subclasses of the class.

Contents


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 reflection
Foo foo = new Foo();
foo.doSomething(1);

// With reflection
Class 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 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 API to hold a reference to the object representing the Java keyword void.

Atomic wrapper classes

With Java 5.0, additional wrapper classes were introduced in the 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:

! Primitive type !! Wrapper class |- | int || |- | long || |- | boolean || |- | V || |}

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

ja:?????????????





Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article



Related Links in Primitive wrapper class

Search for Primitive wrapper class in Tutorials
Search for Primitive wrapper class in Encyclopedia
Search for Primitive wrapper class in Dictionary
Search for Primitive wrapper class in Open Directory
Search for Primitive wrapper class in Store
Search for Primitive wrapper class in PriceGig



Help build the largest human-edited directory on the web.
Submit a Site - Open Directory Project - Become an Editor

Advertisement

Advertisement



Primitive wrapper class
Primitive_wrapper_class top Primitive_wrapper_class

Home - Add TutorGig to Your Site - Disclaimer

©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement