SlideShare a Scribd company logo
1 of 23
Wrapper Classes
Wrapper Classes
 INTRODUCTION : The following two statements illustrate the difference between a
primitive data type and an object of a wrapper class:
int x = 25;
Integer y = new Integer(33);
The first statement declares an int variable named x and initializes it with the value 25.
The second statement instantiates an Integer object. The object is initialized with the
value 33 and a reference to the object is assigned to the object variable y.
REASON :One of the reason why we need wrapper is to use them in collections API. On
the other hand the wrapper objects hold much more memory compared to primitive types.
So use primitive types when you need efficiency and use wrapper class when you need
objects instead of primitive types.
The primitive data types are not objects so they do not belong to any class. While storing
in data structures which support only objects, it is required to convert the primitive type
to object first which we can do by using wrapper classes.
Why do we need Wrapper classes
List of Wrapper Classes
The wrapper classes are very useful as they enable you to manipulate primitive data types.
For example, you can take the integer input from the user in the form of a String, and convert
it into integer type using the following statements:
• String str = “100”;
int j = Integer.parseInt(str);
• There are many more methods in the wrapper classes that help you do several operations
with the data types.
• The wrapper classes also have constants like :
MAX_VALUE, MIN_VALUE, NaN (Not a Number),
POSITIVE_INFINITY, and NEGATIVE_INFINITY.
Integer Class
Class Integer is a wrapper for values of type int.
• Integer objects can be constructed with a int value, or a string containing a int value
• The constructors for Integer are shown here:
Integer( int num)
Integer(String str) throws NumberFormatException
• Some methods of the Integer class:
static int parseInt(String str) throws NumberFormatException
int intValue( ) returns the value of the invoking object as a int value.
Integer i1=new Integer(100);
Integer i2=new Integer(“100”);
Few more method of Integer class (These methods also available in Long,
Short, Byte, Float, Double wrapper class)
byteValue()
Returns the value of the invoking object as a byte.
doubleValue()
Returns the value of the invoking object as a double.
floatValue()
Returns the value of the invoking object as a float.
longValue()
Returns the value of the invoking object as a long.
shortValue()
Returns the value of the invoking object as a short.
E.g.
Integer i1=new Integer(20);
double d1=i1.doubleValue
Integer.toBinaryString(int) method
Prototype :
public static String toBinaryString(int i)
• This method is used to create a String representation of the argument
passed, as an unsigned integer in base 2(with no extra leading 0s (zeros)).
• In other words, this method is used to return a String representation of an
integer in its binary form.
Demo – Integer.toBinaryString(int)
import java.util.Scanner;
class DemoBinaryString {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("Enter any integer between 1 and 255");
int i = x.nextInt();
String s = Integer.toBinaryString(i);
System.out.println("The unsigned integer in base 2 of "+i+ " is : "+s);
}
}
Integer.toOctalString(int) method
Prototype :
public static String toOctalString(int i)
This method is used to create a String representation of the integer argument passed, as an
unsigned integer in base 8. The integer value passed to this method is converted to a String of
ASCII digits in Octal, i.e. base 8 with no extra leading 0s(zeros). In short, this method will
convert an integer into an octal number and return it as a string.
Integer.toHexString(int) method
Prototype :
public static String toHexString(int i)
This method is used to create a String representation of the argument passed, as an unsigned
integer in base 16. The integer value passed to this method is converted to a String of ASCII
digits in hexadecimal, i.e. base 16 with no extra leading 0s(zeros). In short, this method is
used to return a String representation of an integer in its hexadecimal form.
Example
valueOf (), toHexString(), toOctalString() and toBinaryString() Methods:
This is another approach to create wrapper objects. We can convert from binary or octal or
hexadecimal before assigning value to wrapper object using two argument constructor. Below
program explains the method in details:
package WrapperIntro;
public class ValueOfDemo {
public static void main(String[] args) {
Integer intWrapper = Integer.valueOf("12345");
//Converting from binary to decimal
Integer intWrapper2 = Integer.valueOf("11011", 2);
//Converting from hexadecimal to decimal
Integer intWrapper3 = Integer.valueOf("D", 16);
System.out.println("Value of intWrapper Object: "+ intWrapper);
System.out.println("Value of intWrapper2 Object: "+ intWrapper2);
System.out.println("Value of intWrapper3 Object: "+ intWrapper3);
System.out.println("Hex value of intWrapper: " + Integer.toHexString(intWrapper));
System.out.println("Binary Value of intWrapper2: "+ Integer.toBinaryString(intWrapper2));
} }
Character Class
Character class is a wrapper class for character data
types.
• The constructor for Character is:
– Character(char c)
– Here, c specifies the character to be wrapped by the
Character object
• After a Character object is created, you can retrieve the
primitive character value from it using:
– char charValue( )
The Character class contains the following constants:
1.MAX_VALUE - The largest character value.
2.MIN_VALUE - The smallest character value.
TYPE - The Class object for char.
Few more functions from Character class:
1.static String toString(char c) - Returns a String object representing the specified char.
2.static char toLowerCase(char ch) - Converts the character argument to lowercase
3.static char toUpperCase(char ch)- Converts the character argument to uppercase .
Boolean Class
The Boolean class is a wrapper class for boolean values
• It has the following constructors:
– Boolean(boolean bValue)
• Here, bValue can be either true or false
– Boolean(String str)
• The object created by this constructor will have the value true or false depending upon the
string value in str – “true” or “false”.
• The value of str can be in upper case or lower case
Float Class
Class Float is a wrapper for floating-point values of type float
• Float objects can be constructed with a float value, or a string containing a floating-point
value.
• The constructors for float are shown here:
Float( float num)
Float( String str) throws NumberFormatException
• Some methods of the Float class:
static Float valueOf( String str) throws NumberFormatException
float floatValue( ) returns the value of the invoking object as a float value
Double Class
Class Double is a wrapper for floating-point values of type double
• Double objects can be constructed with a double value, or a string containing a floating-
point value.
• The constructors for double are shown here:
Double( double num)
Double( String str) throws NumberFormatException
• Some methods of the Double class:
static Double valueOf( String str) throws NumberFormatException
double doubleValue( ) returns the value of the invoking object as a double value
The Long Class
Class Long is a wrapper for values of type long
• Long objects can be constructed with a long value, or a string containing
a long value
• The constructors for long are shown here:
Long( long num)
Long( String str) throws NumberFormatException
• Some methods of the Long class:
static Long valueOf(String str) throws NumberFormatException
long longValue( ) returns the value of the invoking object as a long value
Short
Class Short is a wrapper for values of type short
• Short objects can be constructed with a short value, or a string
containing a long value
• The constructors for short are shown here:
Short( short num)
Short( String str) throws NumberFormatException
• Some methods of the Short class:
static Short valueOf( String str) throws NumberFormatException
short shortValue( ) returns the value of the invoking object as a short value
Byte
Class Byte is a wrapper for values of type byte
• Byte objects can be constructed with a byte value, or a string
containing a long value
• The constructors for byte are shown here:
Byte( byte num)
Byte( String str) throws NumberFormatException
• Some methods of the Byte class:
static Byte valueOf( String str) throws NumberFormatException
byte byteValue( ) returns the value of the invoking object as a byte value.
Java 5.0 introduced automatic conversion between a primitive type and
the corresponding wrapper class.
During assignment , the automatic transformation of primitive type to corresponding
wrapper type is known as autoboxing.
• Primitive types ---------------------------wrapper type
(autoboxing)
• E. g. Integer i1=10;
During assignment , the automatic transformation of wrapper type into their primitive
equivalent is known as Unboxing.
• wrapper type --------------------------- primitive type
(unboxing)
• E. g. int i=0;
i=new Integer(10);
Autoboxing and Unboxing
Boxing conversion converts values of primitive type to corresponding values of reference
type. But the primitive types can not be widened/ Narrowed to the Wrapper classes and vice
versa.
Wrong!!!
byte b = 12;
Integer I1=b;
Wrong!!!
byte b = 12;
Integer I1=(Integer)b;
Right!!!
byte b = 12;
Integer I1=(int)b;
Discussion Questions
Question 1. How to convert a primitive type to wrapper class?
Question 2. How Wrapper classes help overcome the problem of ArrayList’s that they
only hold objects while Primitive types aren’t objects?
Question 3. If we have two Integer objects, Integer a = 127; Integer b = 127; Why does
a == b evaluate to true when both are holding two separate objects?
Choose the correct option
Q 1. All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character) in
Java are _____________:
a)Final
b)Serializable
c)Immutatble and final
d)Private
Q 2. Which of these is a super class of wrappers Long, Character & Integer?
a)Long
b)Digits
c)Float
d)Number
THANK YOU…

More Related Content

Similar to DAY_1.3.pptx

Similar to DAY_1.3.pptx (20)

Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Dr. Rajeshree Khande - Java Interactive input
Dr. Rajeshree Khande - Java Interactive inputDr. Rajeshree Khande - Java Interactive input
Dr. Rajeshree Khande - Java Interactive input
 
Dr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive inputDr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive input
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Lecture 3.pptx
Lecture 3.pptxLecture 3.pptx
Lecture 3.pptx
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Java String
Java String Java String
Java String
 
Built-in Classes in JAVA
Built-in Classes in JAVABuilt-in Classes in JAVA
Built-in Classes in JAVA
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth session
 
Getting started with C# Programming
Getting started with C# ProgrammingGetting started with C# Programming
Getting started with C# Programming
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 

Recently uploaded

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 

DAY_1.3.pptx

  • 2.  INTRODUCTION : The following two statements illustrate the difference between a primitive data type and an object of a wrapper class: int x = 25; Integer y = new Integer(33); The first statement declares an int variable named x and initializes it with the value 25. The second statement instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable y. REASON :One of the reason why we need wrapper is to use them in collections API. On the other hand the wrapper objects hold much more memory compared to primitive types. So use primitive types when you need efficiency and use wrapper class when you need objects instead of primitive types. The primitive data types are not objects so they do not belong to any class. While storing in data structures which support only objects, it is required to convert the primitive type to object first which we can do by using wrapper classes. Why do we need Wrapper classes
  • 3. List of Wrapper Classes
  • 4. The wrapper classes are very useful as they enable you to manipulate primitive data types. For example, you can take the integer input from the user in the form of a String, and convert it into integer type using the following statements: • String str = “100”; int j = Integer.parseInt(str); • There are many more methods in the wrapper classes that help you do several operations with the data types. • The wrapper classes also have constants like : MAX_VALUE, MIN_VALUE, NaN (Not a Number), POSITIVE_INFINITY, and NEGATIVE_INFINITY.
  • 5. Integer Class Class Integer is a wrapper for values of type int. • Integer objects can be constructed with a int value, or a string containing a int value • The constructors for Integer are shown here: Integer( int num) Integer(String str) throws NumberFormatException • Some methods of the Integer class: static int parseInt(String str) throws NumberFormatException int intValue( ) returns the value of the invoking object as a int value.
  • 6. Integer i1=new Integer(100); Integer i2=new Integer(“100”); Few more method of Integer class (These methods also available in Long, Short, Byte, Float, Double wrapper class) byteValue() Returns the value of the invoking object as a byte. doubleValue() Returns the value of the invoking object as a double. floatValue() Returns the value of the invoking object as a float. longValue() Returns the value of the invoking object as a long. shortValue() Returns the value of the invoking object as a short. E.g. Integer i1=new Integer(20); double d1=i1.doubleValue
  • 7. Integer.toBinaryString(int) method Prototype : public static String toBinaryString(int i) • This method is used to create a String representation of the argument passed, as an unsigned integer in base 2(with no extra leading 0s (zeros)). • In other words, this method is used to return a String representation of an integer in its binary form.
  • 8. Demo – Integer.toBinaryString(int) import java.util.Scanner; class DemoBinaryString { public static void main(String[] args) { Scanner x = new Scanner(System.in); System.out.println("Enter any integer between 1 and 255"); int i = x.nextInt(); String s = Integer.toBinaryString(i); System.out.println("The unsigned integer in base 2 of "+i+ " is : "+s); } }
  • 9. Integer.toOctalString(int) method Prototype : public static String toOctalString(int i) This method is used to create a String representation of the integer argument passed, as an unsigned integer in base 8. The integer value passed to this method is converted to a String of ASCII digits in Octal, i.e. base 8 with no extra leading 0s(zeros). In short, this method will convert an integer into an octal number and return it as a string.
  • 10. Integer.toHexString(int) method Prototype : public static String toHexString(int i) This method is used to create a String representation of the argument passed, as an unsigned integer in base 16. The integer value passed to this method is converted to a String of ASCII digits in hexadecimal, i.e. base 16 with no extra leading 0s(zeros). In short, this method is used to return a String representation of an integer in its hexadecimal form.
  • 11. Example valueOf (), toHexString(), toOctalString() and toBinaryString() Methods: This is another approach to create wrapper objects. We can convert from binary or octal or hexadecimal before assigning value to wrapper object using two argument constructor. Below program explains the method in details: package WrapperIntro; public class ValueOfDemo { public static void main(String[] args) { Integer intWrapper = Integer.valueOf("12345"); //Converting from binary to decimal Integer intWrapper2 = Integer.valueOf("11011", 2); //Converting from hexadecimal to decimal Integer intWrapper3 = Integer.valueOf("D", 16); System.out.println("Value of intWrapper Object: "+ intWrapper); System.out.println("Value of intWrapper2 Object: "+ intWrapper2); System.out.println("Value of intWrapper3 Object: "+ intWrapper3); System.out.println("Hex value of intWrapper: " + Integer.toHexString(intWrapper)); System.out.println("Binary Value of intWrapper2: "+ Integer.toBinaryString(intWrapper2)); } }
  • 12. Character Class Character class is a wrapper class for character data types. • The constructor for Character is: – Character(char c) – Here, c specifies the character to be wrapped by the Character object • After a Character object is created, you can retrieve the primitive character value from it using: – char charValue( ) The Character class contains the following constants: 1.MAX_VALUE - The largest character value. 2.MIN_VALUE - The smallest character value. TYPE - The Class object for char. Few more functions from Character class: 1.static String toString(char c) - Returns a String object representing the specified char. 2.static char toLowerCase(char ch) - Converts the character argument to lowercase 3.static char toUpperCase(char ch)- Converts the character argument to uppercase .
  • 13. Boolean Class The Boolean class is a wrapper class for boolean values • It has the following constructors: – Boolean(boolean bValue) • Here, bValue can be either true or false – Boolean(String str) • The object created by this constructor will have the value true or false depending upon the string value in str – “true” or “false”. • The value of str can be in upper case or lower case
  • 14. Float Class Class Float is a wrapper for floating-point values of type float • Float objects can be constructed with a float value, or a string containing a floating-point value. • The constructors for float are shown here: Float( float num) Float( String str) throws NumberFormatException • Some methods of the Float class: static Float valueOf( String str) throws NumberFormatException float floatValue( ) returns the value of the invoking object as a float value
  • 15. Double Class Class Double is a wrapper for floating-point values of type double • Double objects can be constructed with a double value, or a string containing a floating- point value. • The constructors for double are shown here: Double( double num) Double( String str) throws NumberFormatException • Some methods of the Double class: static Double valueOf( String str) throws NumberFormatException double doubleValue( ) returns the value of the invoking object as a double value
  • 16. The Long Class Class Long is a wrapper for values of type long • Long objects can be constructed with a long value, or a string containing a long value • The constructors for long are shown here: Long( long num) Long( String str) throws NumberFormatException • Some methods of the Long class: static Long valueOf(String str) throws NumberFormatException long longValue( ) returns the value of the invoking object as a long value
  • 17. Short Class Short is a wrapper for values of type short • Short objects can be constructed with a short value, or a string containing a long value • The constructors for short are shown here: Short( short num) Short( String str) throws NumberFormatException • Some methods of the Short class: static Short valueOf( String str) throws NumberFormatException short shortValue( ) returns the value of the invoking object as a short value
  • 18. Byte Class Byte is a wrapper for values of type byte • Byte objects can be constructed with a byte value, or a string containing a long value • The constructors for byte are shown here: Byte( byte num) Byte( String str) throws NumberFormatException • Some methods of the Byte class: static Byte valueOf( String str) throws NumberFormatException byte byteValue( ) returns the value of the invoking object as a byte value.
  • 19. Java 5.0 introduced automatic conversion between a primitive type and the corresponding wrapper class. During assignment , the automatic transformation of primitive type to corresponding wrapper type is known as autoboxing. • Primitive types ---------------------------wrapper type (autoboxing) • E. g. Integer i1=10; During assignment , the automatic transformation of wrapper type into their primitive equivalent is known as Unboxing. • wrapper type --------------------------- primitive type (unboxing) • E. g. int i=0; i=new Integer(10); Autoboxing and Unboxing
  • 20. Boxing conversion converts values of primitive type to corresponding values of reference type. But the primitive types can not be widened/ Narrowed to the Wrapper classes and vice versa. Wrong!!! byte b = 12; Integer I1=b; Wrong!!! byte b = 12; Integer I1=(Integer)b; Right!!! byte b = 12; Integer I1=(int)b;
  • 21. Discussion Questions Question 1. How to convert a primitive type to wrapper class? Question 2. How Wrapper classes help overcome the problem of ArrayList’s that they only hold objects while Primitive types aren’t objects? Question 3. If we have two Integer objects, Integer a = 127; Integer b = 127; Why does a == b evaluate to true when both are holding two separate objects?
  • 22. Choose the correct option Q 1. All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character) in Java are _____________: a)Final b)Serializable c)Immutatble and final d)Private Q 2. Which of these is a super class of wrappers Long, Character & Integer? a)Long b)Digits c)Float d)Number