Junior — Middle
Can you name the main types of wrapper classes and describe their purpose?
sobes.tech AI
Answer from AI
In Java, the main wrapper classes are designed to encapsulate primitive data types into objects. This allows primitives to be used where objects are required, such as in collections.
Main wrapper classes:
Integer— forintDouble— fordoubleBoolean— forbooleanCharacter— forcharLong— forlongFloat— forfloatShort— forshortByte— forbyte
Purpose:
- Allow primitives to be used as objects.
- Provide methods for conversion and working with the respective types.
- Used in collections, for example,
List<Integer>.
Example:
int a = 5;
Integer wrappedA = Integer.valueOf(a); // boxing
int b = wrappedA.intValue(); // unboxing