Wrapper Class - Notes By ShariqSP
Introduction to Wrapper Classes
In Java, wrapper classes provide a way to represent primitive data types as objects. They encapsulate primitive data types within an object, allowing them to be used in situations where objects are required. Wrapper classes also provide utility methods to perform various operations on primitive data types.
Wrapper Classes in Java:
- Byte
- Short
- Integer
- Long
- Float
- Double
- Character
- Boolean
Methods of Wrapper Classes:
Byte:
- parseByte(String s): Parses the string argument as a signed byte.
- byteValue(): Returns the value of this Byte as a byte primitive.
- compareTo(Byte anotherByte): Compares two Byte objects numerically.
- valueOf(String s): Returns a Byte object holding the value given by the specified String.
- toString(): Returns a String object representing this Byte's value.
- hashCode(): Returns a hash code for this Byte object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Short:
- parseShort(String s): Parses the string argument as a signed short.
- shortValue(): Returns the value of this Short as a short primitive.
- compareTo(Short anotherShort): Compares two Short objects numerically.
- valueOf(String s): Returns a Short object holding the value given by the specified String.
- toString(): Returns a String object representing this Short's value.
- hashCode(): Returns a hash code for this Short object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Integer:
- parseInt(String s): Parses the string argument as a signed integer.
- intValue(): Returns the value of this Integer as an int primitive.
- compareTo(Integer anotherInteger): Compares two Integer objects numerically.
- valueOf(String s): Returns an Integer object holding the value given by the specified String.
- toString(): Returns a String object representing this Integer's value.
- hashCode(): Returns a hash code for this Integer object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Long:
- parseLong(String s): Parses the string argument as a signed long.
- longValue(): Returns the value of this Long as a long primitive.
- compareTo(Long anotherLong): Compares two Long objects numerically.
- valueOf(String s): Returns a Long object holding the value given by the specified String.
- toString(): Returns a String object representing this Long's value.
- hashCode(): Returns a hash code for this Long object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Float:
- parseFloat(String s): Parses the string argument as a signed float.
- floatValue(): Returns the value of this Float as a float primitive.
- compareTo(Float anotherFloat): Compares two Float objects numerically.
- valueOf(String s): Returns a Float object holding the value given by the specified String.
- toString(): Returns a String object representing this Float's value.
- hashCode(): Returns a hash code for this Float object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Double:
- parseDouble(String s): Parses the string argument as a signed double.
- doubleValue(): Returns the value of this Double as a double primitive.
- compareTo(Double anotherDouble): Compares two Double objects numerically.
- valueOf(String s): Returns a Double object holding the value given by the specified String.
- toString(): Returns a String object representing this Double's value.
- hashCode(): Returns a hash code for this Double object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Character:
- charValue(): Returns the value of this Character as a char primitive.
- compareTo(Character anotherCharacter): Compares two Character objects numerically.
- valueOf(char c): Returns a Character instance representing the specified char value.
- toString(): Returns a String object representing this Character's value.
- hashCode(): Returns a hash code for this Character object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Boolean:
- booleanValue(): Returns the value of this Boolean object as a boolean primitive.
- compareTo(Boolean anotherBoolean): Compares two Boolean objects numerically.
- valueOf(boolean b): Returns a Boolean instance representing the specified boolean value.
- toString(): Returns a String object representing this Boolean's value.
- hashCode(): Returns a hash code for this Boolean object.
- equals(Object obj): Compares this object to the specified object.
- getClass(): Returns the Class object that represents the runtime class of this object.
- notify(): Wakes up a single thread that is waiting on this object's monitor.
- notifyAll(): Wakes up all threads that are waiting on this object's monitor.
- wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
- wait(long timeout): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
- wait(long timeout, int nanos): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
Interview Questions on Wrapper Classes in Java
Interview Questions:
- What are wrapper classes in Java?
- Explain the purpose of wrapper classes.
- What are the different types of wrapper classes available in Java?
- What is autoboxing and unboxing in Java?
- When should you use primitive types, and when should you use wrapper classes?
- Explain the significance of autoboxing and unboxing in collection frameworks.
- What are some important methods provided by wrapper classes?
- How do you convert between primitive types and their corresponding wrapper classes?
- What is the purpose of the valueOf() method in wrapper classes?
- What are the limitations of using wrapper classes?
Multiple Choice Questions (MCQs):
- Which of the following is a wrapper class for the primitive type int?
a) Int
b) Integer
c) IntWrapper
d) None of the above
Answer: b) Integer - Which method is used to convert a String to its corresponding wrapper class object?
a) parse()
b) valueOf()
c) toObject()
d) convert()
Answer: b) valueOf() - What is the default value of a wrapper class object if not initialized?
a) null
b) 0
c) false
d) Default value of the corresponding primitive type
Answer: a) null - Which method is used to obtain the primitive value of a wrapper class object?
a) get()
b) primitiveValue()
c) getValue()
d) intValue()
Answer: d) intValue() - Wrapper classes are used to:
a) Wrap primitive types into objects
b) Unwrap objects into primitive types
c) Provide utility methods for primitive types
d) All of the above
Answer: d) All of the above