Wrapper Classes in Java
Primitive | Wrapper Class |
---|---|
boolean | Boolean |
int | Integer |
float | Float |
char | Character |
byte | Byte |
long | Long |
short | Short |
Wrapper classes in Java are what we use to encapsulate primitive data types and convert them into objects. They provide a way for you to treat primitive types as objects, enabling them to be used in scenarios that require objects, such as collections, generics, and methods that expect objects as arguments.
Boolean: This wrapper class is what you use to encapsulate the boolean primitive type. It provides methods for working with boolean values, such as converting strings to booleans and performing logical operations.
Integer: When you need to work with int, the Integer class wraps the int primitive type. It provides various methods for converting strings to integers, performing arithmetic operations, and working with integer values.
Float: The Float class is there for you when you need to wrap the float primitive type. It offers methods for converting strings to floating-point numbers and performing arithmetic operations.
Character: This wrapper class is used when you need to encapsulate the char primitive type. It provides methods for working with individual characters, converting characters to strings, and performing character-related operations.
Byte: You use the Byte class to wrap the byte primitive type. It is useful for converting strings to bytes and performing byte-related operations.
Long: When you need to wrap the long primitive type, you use the Long class. It offers methods for converting strings to long integers and performing arithmetic operations with long values.
Short: This wrapper class is used when you need to encapsulate the short primitive type. It provides methods for converting strings to short integers and performing short-related operations.
By using wrapper classes, we can use primitives wherever objects are required and take advantage of the additional functionality provided by the wrapper classes.