- Primitive Types are special data types built into the language. They are not objects created from a class.
- Wrapper Classes wrap the primitive data type into an Object of its Class.
- An instance of a primitive class holds the actual value of the instance, but instance of a wrapper class holds a reference to the object.
- Wrapper class can be null but Primitive Types can't be null. Ex: Integer can be null, int can't.
- Wrapper Classes have utility functions such as conversion from one type to another, which are not available in Primitive Data Types.
- Wrapper classes other than Boolean have constants MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
When to use Primitive Type?
- You should use primitive types unless you need an object.
- Wrapper classes are less efficient than primitive data types because the value is wrapped inside an object. For example, an ArrayList<Integer> is far less efficient than an int[] array.
When to use Wrappers?
- When a function expects Object as a parameter, only Wrapper class objects can be passed.
- Collections cannot be created of primitive data types. The type parameter inside the angle brackets cannot be a primitive type. User Wrapper Classes.
No comments:
Post a Comment