About Lesson
Primitive Types
Primitive types in Java are predefined data types that represent simple values, and they are not objects. There are eight primitive types in Java:
byte
: 8-bit signed integer.short
: 16-bit signed integer.int
: 32-bit signed integer.long
: 64-bit signed integer.float
: 32-bit floating-point number.double
: 64-bit floating-point number.char
: 16-bit Unicode character.boolean
: Represents true or false values.
Wrapper Classes
Wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. They provide utility methods and constructors to convert primitive types into objects and vice versa. Each primitive type has a corresponding wrapper class:
Byte
: Representsbyte
.Short
: Representsshort
.Integer
: Representsint
.Long
: Representslong
.Float
: Representsfloat
.Double
: Representsdouble
.Character
: Representschar
.Boolean
: Representsboolean
.
Uses of Wrapper Classe
- Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
- java.util package: The java.util package provides the utility classes to deal with objects.
- Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.
- Wrapper classes often define constants and enumerations for commonly used values and types, providing a convenient way to access them.
- Primitive types cannot be
null
, but wrapper classes can benull
, allowing them to represent absence of value in scenarios like database operations or optional method parameters.