[go: up one dir, main page]

0% found this document useful (0 votes)
10 views16 pages

wrapper classes

Wrapper classes in Java provide object representations for each primitive type, allowing for class-type behavior and the use of useful constants and static methods. Boxing and unboxing processes enable conversion between primitive values and their corresponding wrapper objects, with automatic boxing and unboxing introduced in Java 5. Additionally, wrapper classes contain static methods for parsing strings to numeric types and vice versa, as well as methods for string processing in the Character class.

Uploaded by

saradha.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

wrapper classes

Wrapper classes in Java provide object representations for each primitive type, allowing for class-type behavior and the use of useful constants and static methods. Boxing and unboxing processes enable conversion between primitive values and their corresponding wrapper objects, with automatic boxing and unboxing introduced in Java 5. Additionally, wrapper classes contain static methods for parsing strings to numeric types and vice versa, as well as methods for string processing in the Character class.

Uploaded by

saradha.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Wrapper Classes

• Wrapper classes provide a class type corresponding


to each of the primitive types
– This makes it possible to have class types that behave
somewhat like primitive types
– The wrapper classes for the primitive types byte, short,
long, float, double, and char are (in order) Byte,
Short, Long, Float, Double, and Character
• Wrapper classes also contain a number of useful
predefined constants and static methods

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-1


Wrapper Classes
• The java.lang package contains a wrapper class
that corresponds to each primitive type:
Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
void Void 2
Wrapper Classes
• The following declaration creates an Integer
object which is a reference to an object with the
integer value 40
Integer age = new Integer(40);
• An object of a wrapper class is used in situations
where a primitive value will not suffice
• For example, some objects serve as containers of
other objects
• Primitive values could not be stored in such
containers, but wrapper objects could be

3
Wrapper Classes
• Wrapper classes may contain static methods that help
manage the associated type
– For example, the Integer class contains a method to
convert digits stored in a String to an int value:
num = Integer.parseInt(str);
• Wrapper classes often contain useful constants
– For example, the Integer class contains MIN_VALUE and
MAX_VALUE for the smallest and largest int values

4
Wrapper Classes
• Boxing: the process of going from a value of a
primitive type to an object of its wrapper class
– To convert a primitive value to an "equivalent" class type
value, create an object of the corresponding wrapper class
using the primitive value as an argument
– The new object will contain an instance variable that
stores a copy of the primitive value
– Unlike most other classes, a wrapper class does not have a
no-argument constructor
Integer integerObject = new Integer(42);

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-5


Wrapper Classes
• Unboxing: the process of going from an object
of a wrapper class to the corresponding value of
a primitive type
– The methods for converting an object from the
wrapper classes Byte, Short, Integer, Long,
Float, Double, and Character to their
corresponding primitive type are (in order)
byteValue, shortValue, intValue,
longValue, floatValue, doubleValue, and
charValue
– None of these methods take an argument
int i = integerObject.intValue();

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-6


Automatic Boxing and Unboxing
• Starting with version 5.0, Java can automatically do boxing
and unboxing
• Instead of creating a wrapper class object using the new
operation (as shown before), it can be done as an automatic
type cast:
Integer integerObject = 42;
• Instead of having to invoke the appropriate method (such as
intValue, doubleValue, charValue, etc.) in order to
convert from an object of a wrapper class to a value of its
associated primitive type, the primitive value can be
recovered automatically
int i = integerObject;

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-7


Constants and Static Methods in Wrapper
Classes
• Wrapper classes include useful constants that
provide the largest and smallest values for any of the
primitive number types
– For example, Integer.MAX_VALUE,
Integer.MIN_VALUE, Double.MAX_VALUE,
Double.MIN_VALUE, etc.
• The Boolean class has names for two constants of
type Boolean
– Boolean.TRUE and Boolean.FALSE are the Boolean
objects that correspond to the values true and false of
the primitive type boolean

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-8


Constants and Static Methods in Wrapper
Classes
• Wrapper classes have static methods that convert a
correctly formed string representation of a number to
the number of a given type
– The methods Integer.parseInt, Long.parseLong,
Float.parseFloat, and Double.parseDouble do this
for the primitive types (in order) int, long, float, and
double
• Wrapper classes also have static methods that convert
from a numeric value to a string representation of the
value
– For example, the expression
Double.toString(123.99);
returns the string value "123.99"
• The Character class contains a number of static
methods that are useful for string processing
Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-9
Some Methods in the Class Character (Part 1
of 3)

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-10


Some Methods in the Class Character (Part 2
of 3)

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-11


Some Methods in the Class Character (Part 3
of 3)

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5-12


The StringTokenizer Class
• The StringTokenizer class is used to
recover the words or tokens in a multi-word
String
– You can use whitespace characters to separate each
token, or you can specify the characters you wish to
use as separators
– In order to use the StringTokenizer class, be
sure to include the following at the start of the file:
import java.util.StringTokenizer;

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 4-13


Some Methods in the StringTokenizer
Class (Part 1 of 2)

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 4-14


Some Methods in the StringTokenizer
Class (Part 2 of 2)

Copyright © 2008 Pearson Addison-Wesley. All rights reserved 4-15


Exercise 1: Write a program that reads in a line of text and then first
output the total number of words/tokens (separated by whitespace
characters) in that line of text and then individual words one at a line.

• The classes you may want to use


– The Scanner class in java.util package
• Scanner keyboard = new Scanner(System.in);
• public String nextLine();
– The String class in java.lang
– The StringTokenizer class
• pubic StringTokenizer(String theString);
• pubic StringTokenizer(String theString, String delimiters);
• public int countTokens();
• public String nextToken();
• public boolean hasMoreTokens();

You might also like