Autoboxing and unboxing - Autoboxing and unboxing Java type...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Autoboxing and unboxing Java type parameters can only be instantiated with object types, but not with primitive types. This is because the compiler can think about a type parameter simply as a reference to “something”, without having to know what the something is—but that doesn’t work with primitive types. So our Stack interface: public interface Stack<AnyType> { // stack interface } cannot be used to make a stack of integers. Java solves this problem by providing a wrapper class for each of the primitive types, as follows: Primitive type wrapper class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean Each wrapper object is immutable (meaning its state can never change) and stores one primitive value that is set when the object is constructed. As an example, the following code shows how to use a stack of integers. Note carefully that we cannot use Stack<int> , this would cause a compiler error.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.
Ask a homework question - tutors are online