SlideShare a Scribd company logo
Java Primer WrapperClasses-1 © Scott MacKenzie
Wrapper Classes
Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper
classes, because they "wrap" the primitive data type into an object of that class. So, there is an
Integer class that holds an int variable, there is a Double class that holds a double
variable, and so on. The wrapper classes are part of the java.lang package, which is imported
by default into all Java programs. The following discussion focuses on the Integer wrapper
class, but applies in a general sense to all eight wrapper classes. Consult the Java API
documentation for more details.
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. The memory assignments from
these two statements are visualized in Figure 1.
(a) (b)
25
x y
33
Figure 1. Variables vs. objects (a) declaration and initialization
of an int variable (b) instantiation of an Integer object
Clearly x and y differ by more than their values: x is a variable that holds a value; y is an object
variable that holds a reference to an object. As noted earlier, data fields in objects are not, in
general, directly accessible. So, the following statement using x and y as declared above is not
allowed:
int z = x + y; // wrong!
The data field in an Integer object is only accessible using the methods of the Integer class.
One such method — the intValue() method — returns an int equal to the value of the
object, effectively "unwrapping" the Integer object:
int z = x + y.intValue(); // OK!
Note the format of the method call. The intValue() method is an instance method, because it
is called through an instance of the class — an Integer object. The syntax uses dot notation,
where the name of the object variable precedes the dot.
Some of Java's classes include only instance methods, some include only class methods, some
include both. The Integer class includes both instance methods and class methods. The class
methods provide useful services; however, they are not called through instances of the class.
But, this is not new. We met a class method of the Integer class earlier. The statement
int x = Integer.parseInt("1234");
converts the string "1234" into the integer 1,234 and assigns the result to the int variable x.
The method parseInt() is a class method. It is not called through an instance of the class.
Java Primer WrapperClasses-2 © Scott MacKenzie
Although dot notation is used above, the term "Integer." identifies the class where the method
is defined, rather than name of an Integer object. This is one of the trickier aspects of
distinguishing instance methods from class methods. For an instance method, the dot is preceded
by the name of an object variable. For a class method, the dot is preceded by the name of a class.
It is interesting that the parseInt() method accepts a String argument and returns an int.
Neither the argument nor the returned value is an Integer object. So, the earlier comment that
class methods "provide useful services" is quite true. They do not operate on instance variables;
they provide services that are of general use. The parseInt() method converts a String to
an int, and this is a useful service — one we have called upon in previous programs.
Another useful Integer class method is the toString() method. The following statement
String s = Integer.toString(1234);
converts the int constant 1,234 to a String object and returns a reference to the object. We
have already met this method through the following shorthand notation:
String s = "" + 1234;
In general, the data fields in objects are private and are only accessible through methods of the
class. However, class definitions sometimes include constants that are "public", and, therefore,
accessible anywhere the class is accessible. Java's wrapper classes include publicly defined
constants identifying the range for each type. For example, the Integer class has constants
called MIN_VALUE and MAX_VALUE equal to 0x80000000 and 0x7fffffff respectively. These
values are the hexadecimal equivalent of the most-negative and most-positive values represented
by 32-bit integers. The table of ranges shown earlier was obtained from a simple program called
FindRanges that prints these constants. The listing is given here in Figure 2.
1 public class FindRanges
2 {
3 public static void main(String[] args)
4 {
5 System.out.println("Integer range:");
6 System.out.println(" min: " + Integer.MIN_VALUE);
7 System.out.println(" max: " + Integer.MAX_VALUE);
8 System.out.println("Double range:");
9 System.out.println(" min: " + Double.MIN_VALUE);
10 System.out.println(" max: " + Double.MAX_VALUE);
11 System.out.println("Long range:");
12 System.out.println(" min: " + Long.MIN_VALUE);
13 System.out.println(" max: " + Long.MAX_VALUE);
14 System.out.println("Short range:");
15 System.out.println(" min: " + Short.MIN_VALUE);
16 System.out.println(" max: " + Short.MAX_VALUE);
17 System.out.println("Byte range:");
18 System.out.println(" min: " + Byte.MIN_VALUE);
19 System.out.println(" max: " + Byte.MAX_VALUE);
20 System.out.println("Float range:");
21 System.out.println(" min: " + Float.MIN_VALUE);
22 System.out.println(" max: " + Float.MAX_VALUE);
23 }
24 }
Figure 2. FindRanges.java
This program generates the following output:
Java Primer WrapperClasses-3 © Scott MacKenzie
Integer range:
min: -2147483648
max: 2147483647
Double range:
min: 4.9E-324
max: 1.7976931348623157E308
Long range:
min: -9223372036854775808
max: 9223372036854775807
Short range:
min: -32768
max: 32767
Byte range:
min: -128
max: 127
Float range:
min: 1.4E-45
max: 3.4028235E38
In the case of float and double, the minimum and maximum values are those closest to plus
or minus zero or plus or minus infinity, respectively.
The most common methods of the Integer wrapper class are summarized in Table 1. Similar
methods for the other wrapper classes are found in the Java API documentation.
Table 1. Methods of the Integer Wrapper Class
Method Purpose
Constructors
Integer(i) constructs an Integer object equivalent to the integer i
Integer(s) constructs an Integer object equivalent to the string s
Class Methods
parseInt(s) returns a signed decimal integer value equivalent to string s
toString(i) returns a new String object representing the integer i
Instance Methods
byteValue() returns the value of this Integer as a byte
doubleValue() returns the value of this Integer as an double
floatValue() returns the value of this Integer as a float
intValue() returns the value of this Integer as an int
longValue() returns the value of this Integer as a long
shortValue() returns the value of this Integer as a short
toString() returns a String object representing the value of this Integer
In the descriptions of the instance methods, the phrase "this Integer" refers to the instance
variable on which the method is acting. For example, if y is an Integer object variable, the
expression y.doubleValue() returns the value of "this Integer", or y, as a double.

More Related Content

PPS
Wrapper class
PDF
Java Wrapper Classes and I/O Mechanisms
PDF
Farhaan Ahmed, BCA 2nd Year
PPTX
JSpiders - Wrapper classes
PPTX
Autoboxing And Unboxing In Java
PPTX
Vectors in Java
PDF
Built in classes in java
Wrapper class
Java Wrapper Classes and I/O Mechanisms
Farhaan Ahmed, BCA 2nd Year
JSpiders - Wrapper classes
Autoboxing And Unboxing In Java
Vectors in Java
Built in classes in java

What's hot (20)

PPTX
L9 wrapper classes
PPT
Wrapper class (130240116056)
PPS
String and string buffer
PPT
wrapper classes
PPTX
Java String
PPS
Class method
DOCX
Fundamental classes in java
PDF
Lecture20 vector
PDF
Wrapper classes
PPTX
Function Java Vector class
DOCX
Autoboxing and unboxing
PDF
OOPs & Inheritance Notes
PPT
String handling session 5
PPT
Chapter 9 - Characters and Strings
PPT
Chapter 4 - Defining Your Own Classes - Part I
PPT
Chapter 2 - Getting Started with Java
PPT
String classes and its methods.20
PPT
Chapter 7 - Defining Your Own Classes - Part II
PPTX
Java tutorial part 3
PPTX
String, string builder, string buffer
L9 wrapper classes
Wrapper class (130240116056)
String and string buffer
wrapper classes
Java String
Class method
Fundamental classes in java
Lecture20 vector
Wrapper classes
Function Java Vector class
Autoboxing and unboxing
OOPs & Inheritance Notes
String handling session 5
Chapter 9 - Characters and Strings
Chapter 4 - Defining Your Own Classes - Part I
Chapter 2 - Getting Started with Java
String classes and its methods.20
Chapter 7 - Defining Your Own Classes - Part II
Java tutorial part 3
String, string builder, string buffer
Ad

Similar to Wrapper classes (20)

PPTX
Hemajava
PPTX
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
PPTX
Java Unit 2(Part 1)
PPTX
Ch-2ppt.pptx
PPTX
Classes objects in java
PPTX
Classes,object and methods java
PPTX
Core Java and Data Structure using C++.pptx
PPT
packages and interfaces
PPTX
classes-objects in oops java-201023154255.pptx
PPTX
Java As an OOP Language,Exception Handling & Applets
PPT
JAVA CONCEPTS
DOC
08review (1)
PPTX
DOC-20240812-WA0000 array string and.pptx
PPT
Object and class
PDF
Built-in Classes in JAVA
PPTX
Collections
PPTX
Classes,object and methods jav
PPTX
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
PPT
Arrays in Java Programming Language slides
Hemajava
M251_Meeting_ jAVAAAAAAAAAAAAAAAAAA.pptx
Java Unit 2(Part 1)
Ch-2ppt.pptx
Classes objects in java
Classes,object and methods java
Core Java and Data Structure using C++.pptx
packages and interfaces
classes-objects in oops java-201023154255.pptx
Java As an OOP Language,Exception Handling & Applets
JAVA CONCEPTS
08review (1)
DOC-20240812-WA0000 array string and.pptx
Object and class
Built-in Classes in JAVA
Collections
Classes,object and methods jav
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
Arrays in Java Programming Language slides
Ad

More from simarsimmygrewal (9)

DOCX
Java file
DOCX
DOCX
Java file
DOCX
DOCX
PPTX
Handling inputs via scanner class
PPTX
Handling inputs via io..continue
PPTX
Excception handling
PPTX
Type casting
Java file
Java file
Handling inputs via scanner class
Handling inputs via io..continue
Excception handling
Type casting

Recently uploaded (20)

PDF
Trump Administration's workforce development strategy
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
Trump Administration's workforce development strategy
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Cambridge-Practice-Tests-for-IELTS-12.docx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
International_Financial_Reporting_Standa.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Computer Architecture Input Output Memory.pptx
Empowerment Technology for Senior High School Guide
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
What if we spent less time fighting change, and more time building what’s rig...
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Share_Module_2_Power_conflict_and_negotiation.pptx
Unit 4 Computer Architecture Multicore Processor.pptx

Wrapper classes

  • 1. Java Primer WrapperClasses-1 © Scott MacKenzie Wrapper Classes Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class. So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs. The following discussion focuses on the Integer wrapper class, but applies in a general sense to all eight wrapper classes. Consult the Java API documentation for more details. 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. The memory assignments from these two statements are visualized in Figure 1. (a) (b) 25 x y 33 Figure 1. Variables vs. objects (a) declaration and initialization of an int variable (b) instantiation of an Integer object Clearly x and y differ by more than their values: x is a variable that holds a value; y is an object variable that holds a reference to an object. As noted earlier, data fields in objects are not, in general, directly accessible. So, the following statement using x and y as declared above is not allowed: int z = x + y; // wrong! The data field in an Integer object is only accessible using the methods of the Integer class. One such method — the intValue() method — returns an int equal to the value of the object, effectively "unwrapping" the Integer object: int z = x + y.intValue(); // OK! Note the format of the method call. The intValue() method is an instance method, because it is called through an instance of the class — an Integer object. The syntax uses dot notation, where the name of the object variable precedes the dot. Some of Java's classes include only instance methods, some include only class methods, some include both. The Integer class includes both instance methods and class methods. The class methods provide useful services; however, they are not called through instances of the class. But, this is not new. We met a class method of the Integer class earlier. The statement int x = Integer.parseInt("1234"); converts the string "1234" into the integer 1,234 and assigns the result to the int variable x. The method parseInt() is a class method. It is not called through an instance of the class.
  • 2. Java Primer WrapperClasses-2 © Scott MacKenzie Although dot notation is used above, the term "Integer." identifies the class where the method is defined, rather than name of an Integer object. This is one of the trickier aspects of distinguishing instance methods from class methods. For an instance method, the dot is preceded by the name of an object variable. For a class method, the dot is preceded by the name of a class. It is interesting that the parseInt() method accepts a String argument and returns an int. Neither the argument nor the returned value is an Integer object. So, the earlier comment that class methods "provide useful services" is quite true. They do not operate on instance variables; they provide services that are of general use. The parseInt() method converts a String to an int, and this is a useful service — one we have called upon in previous programs. Another useful Integer class method is the toString() method. The following statement String s = Integer.toString(1234); converts the int constant 1,234 to a String object and returns a reference to the object. We have already met this method through the following shorthand notation: String s = "" + 1234; In general, the data fields in objects are private and are only accessible through methods of the class. However, class definitions sometimes include constants that are "public", and, therefore, accessible anywhere the class is accessible. Java's wrapper classes include publicly defined constants identifying the range for each type. For example, the Integer class has constants called MIN_VALUE and MAX_VALUE equal to 0x80000000 and 0x7fffffff respectively. These values are the hexadecimal equivalent of the most-negative and most-positive values represented by 32-bit integers. The table of ranges shown earlier was obtained from a simple program called FindRanges that prints these constants. The listing is given here in Figure 2. 1 public class FindRanges 2 { 3 public static void main(String[] args) 4 { 5 System.out.println("Integer range:"); 6 System.out.println(" min: " + Integer.MIN_VALUE); 7 System.out.println(" max: " + Integer.MAX_VALUE); 8 System.out.println("Double range:"); 9 System.out.println(" min: " + Double.MIN_VALUE); 10 System.out.println(" max: " + Double.MAX_VALUE); 11 System.out.println("Long range:"); 12 System.out.println(" min: " + Long.MIN_VALUE); 13 System.out.println(" max: " + Long.MAX_VALUE); 14 System.out.println("Short range:"); 15 System.out.println(" min: " + Short.MIN_VALUE); 16 System.out.println(" max: " + Short.MAX_VALUE); 17 System.out.println("Byte range:"); 18 System.out.println(" min: " + Byte.MIN_VALUE); 19 System.out.println(" max: " + Byte.MAX_VALUE); 20 System.out.println("Float range:"); 21 System.out.println(" min: " + Float.MIN_VALUE); 22 System.out.println(" max: " + Float.MAX_VALUE); 23 } 24 } Figure 2. FindRanges.java This program generates the following output:
  • 3. Java Primer WrapperClasses-3 © Scott MacKenzie Integer range: min: -2147483648 max: 2147483647 Double range: min: 4.9E-324 max: 1.7976931348623157E308 Long range: min: -9223372036854775808 max: 9223372036854775807 Short range: min: -32768 max: 32767 Byte range: min: -128 max: 127 Float range: min: 1.4E-45 max: 3.4028235E38 In the case of float and double, the minimum and maximum values are those closest to plus or minus zero or plus or minus infinity, respectively. The most common methods of the Integer wrapper class are summarized in Table 1. Similar methods for the other wrapper classes are found in the Java API documentation. Table 1. Methods of the Integer Wrapper Class Method Purpose Constructors Integer(i) constructs an Integer object equivalent to the integer i Integer(s) constructs an Integer object equivalent to the string s Class Methods parseInt(s) returns a signed decimal integer value equivalent to string s toString(i) returns a new String object representing the integer i Instance Methods byteValue() returns the value of this Integer as a byte doubleValue() returns the value of this Integer as an double floatValue() returns the value of this Integer as a float intValue() returns the value of this Integer as an int longValue() returns the value of this Integer as a long shortValue() returns the value of this Integer as a short toString() returns a String object representing the value of this Integer In the descriptions of the instance methods, the phrase "this Integer" refers to the instance variable on which the method is acting. For example, if y is an Integer object variable, the expression y.doubleValue() returns the value of "this Integer", or y, as a double.