Lecture 11 LPU CSE 310 Programming in Java













![Boxing and Unboxing class Auto. Box { public static void main(String args[]) { Integer Boxing and Unboxing class Auto. Box { public static void main(String args[]) { Integer](https://slidetodoc.com/presentation_image_h2/903db80bb10837bc2386c64e05c66379/image-14.jpg)
- Slides: 14

Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Outcome of the Lecture Upon completion of this lecture you will be able to ü Find the Utility of Wrapper Classes ü Use Wrapper Classes ü Understand the concept of Boxing and Unboxing Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Outline of the Presentation ü Wrapper Classes ü Inheritance Hierarchy of Wrapper Classes ü Double and Float Classes ü Integer Classes ü Character Classes ü Boolean Classes ü Boxing and Unboxing Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Wrapper Classes ü Primitive data types are not used as objects in Java ü However, many Java methods require the use of objects as arguments. For example, the add(object) method in the Array. List ü Java offers a convenient way to incorporate, or wrap, a primitive data type into an object. Corresponding class is called a Wrapper Class ü For example, wrapping into the Integer class, and wrapping double into the Double class ü Most wrapper class names for a primitive type are the same as the primitive data type name with the first letter capitalized. The exceptions are Integer and Character. ü Java provides Boolean, Character, Double, Float, Byte, Short, Integer, and Long wrapper classes for primitive data types ü These classes are grouped in the java. lang package. Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Inheritance Hierarchy of Wrapper Classes Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Double and Float Constructors Double(double num) Double(String str) throws Number. Format. Exception Float(double num) Float(float num) Float(String str) throws Number. Format. Exception Constants ( static ) MAX_VALUE MIN_VALUE Na. N POSITIVE_INFINITY NEGATIVE_INFINITY TYPE SIZE Lecture 11 Maximum positive value Minimum positive value Not a number Positive infinity Negative infinity The Class object for float or double Number of bits used to represent double type © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Double and Float Methods in java. lang. Double static int compare(double d 1, double d 2) int compare. To(Double another. Double) boolean equals(Object obj) boolean is. Infinite() boolean is. Na. N() static double parse. Double(String s) String to. String() static Double value. Of(String s) static String to. Hex. String(double d) Similar methods in java. lang. Float but with float or Float argument Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Integer Constructors Integer(int value) Integer(String s) Constants (static) Lecture 11 MAX_VALUE Maximum positive value MIN_VALUE Minimum positive value TYPE The Class object for float or double SIZE Number of bits used to represent int type © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Integer Methods in java. lang. Integer int compare. To(Integer another. Integer) double. Value() // similarly int. Value(), byte. Value(), float. Value(), short. Value() boolean equals(Object obj) static int parse. Int(String s, int radix) static String to. Binary. String(int i) // to. Hex. String(), to. Octal. String() String to. String() static String to. String(int i) static Integer value. Of(String s, int radix) Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Character Constructors Character(char ch) Constants (static) MAX_VALUE MIN_VALUE TYPE SIZE Lecture 11 The largest character value The smallest character value The Class object for char The number of bits used to represent a char value © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Character Methods in java. lang. Character static boolean is. Defined(char ch) static boolean is. Digit(char ch) static boolean is. Letter. Or. Digit(char ch) static boolean is. Lower. Case(char ch) static boolean is. Space. Char(char ch) static boolean is. Upper. Case(char ch) static boolean is. Whitespace(char ch) static char to. Lower. Case(char ch) static char to. Upper. Case(char ch) Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Boolean Constructors Boolean(boolean bool. Value) Boolean(String bool. String) Methods in java. lang. Boolean int compare. To(Boolean b) boolean equals(Object bool. Obj) static boolean parse. Boolean(String str) String to. String( ) static String to. String(boolean bool. Val) static Boolean value. Of(String bool. String) Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Boxing and Unboxing ü Java allows primitive types and wrapper classes to be converted automatically ü Converting a primitive value to a wrapper object is called Boxing ü The reverse conversion is called Unboxing Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon
![Boxing and Unboxing class Auto Box public static void mainString args Integer Boxing and Unboxing class Auto. Box { public static void main(String args[]) { Integer](https://slidetodoc.com/presentation_image_h2/903db80bb10837bc2386c64e05c66379/image-14.jpg)
Boxing and Unboxing class Auto. Box { public static void main(String args[]) { Integer i. Ob = 100; // autobox an int i = i. Ob; // auto-unbox System. out. println(i + " " + i. Ob); } } displays 100 Lecture 11 © LPU : : CSE 310 Programming in Java : : Sawal Tandon