Wrapper Classes - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Wrapper Classes

Description:

Double dd = new Double (1.0); 6/14/09. cs141_04 - Wrapper Classes. 7 ... dd = new Double (1.0); double d = dd.doubleValue ... Double dd = Double.valueOf ( s ) ... – PowerPoint PPT presentation

Number of Views:375
Avg rating:3.0/5.0
Slides: 15
Provided by: calpoly
Category:
Tags: classes | dd | wrapper

less

Transcript and Presenter's Notes

Title: Wrapper Classes


1
Wrapper Classes
  • What are Wrapper Classes
  • Converting Data Types to Objects
  • Converting Objects to Data Types
  • Strings and Wrapper Objects
  • Robert Stumpf, Ph.D.
  • CIS Department

2
What are Wrapper Classes
  • It is possible to have an int as a data type
  • Also it is possible to have an Integer Object
  • Java has the benefits of both storage methods
  • Data types are efficient while Objects have extra
    behaviors

3
What are Wrapper Classes
  • All data types have the ability to be inside an
    object with a corresponding name
  • This is called wrapping a data type inside an
    object
  • A primary reason for this is many classes
    (Collections) for example can only contain objects

4
What are Wrapper Classes
  • Java has both data types and objects
  • boolean Boolean
  • char Char
  • int Integer
  • double Double
  • etc..

5
What are Wrapper Classes
  • Thus the corresponding class name begins with an
    upper case letter
  • A reason extra behaviors are needed is to make it
    easy to convert data types to objects and objects
    to data types

6
Converting Data Types to Objects
  • To convert from data type to object
  • Boolean bb new Boolean (true)
  • Character cc new Character (a)
  • Integer ii new Integer (1)
  • Double dd new Double (1.0)

7
Converting Data Types to Objects
  • One can convert from data types to strings using
    the built in toString method
  • For example
  • String s Integer.toString (20)
  • or
  • String s Double.toString (20.0)

8
Converting Data Types to Objects
  • One can also convert from data types to strings
    with concatenation
  • For example
  • s Answer is 20
  • This is equivalent to
  • s Answer is Integer.toString (20)
  • This is used frequently in println
  • System.out.println (Answer is 20 )

9
Converting Objects to Data Types
  • To convert from object to data type
  • Boolean bb new Boolean (true)
  • boolean b bb.booleanValue ( )
  • Character cc new Character (a)
  • char c cc.charValue ( )
  • Integer ii new Integer (1)
  • int i ii.intValue ( )
  • Double dd new Double (1.0)
  • double d dd.doubleValue ( )

10
Converting Objects to Data Types
  • To convert from String to data type
  • String s new String (1)
  • int i Integer.parseInt ( s )
  • String s new String (1)
  • long l Long.parseLong ( s )
  • Cant be done for float or double !
  • Note that this is a static (class) method
  • Interesting enough Netscape implements parseFloat
    in Java Script

11
Strings and Wrapper Objects
  • To convert from String to object
  • String s new String (1)
  • Integer ii Integer.valueOf ( s )
  • String s new String (1.0)
  • Double dd Double.valueOf ( s )
  • Note that these are static (class) methods as
    they are sent to the class, not the object

12
Strings and Wrapper Objects
  • To convert from object to String
  • Boolean bb new Boolean (true)
  • String s bb.toString ( )
  • Character cc new Character (a)
  • String s cc.toString ( )
  • Integer ii new Integer (1)
  • String s ii.toString ( )
  • Double dd new Double (1.0)
  • String s dd.toString ( )

13
Summary
  • Arithmetic is done in data types only
  • Objects exist because many utility classes use
    only objects as arguments
  • The complicated part is remembering how to
    convert from one form to another
  • This is twice as complicated when one considers
    Strings
  • Most books treat this subject very lightly

14
Thank You
  • No day in which you learn something is a
    complete loss.Thank you
  • Robert Stumpf
  • email rvstumpf_at_csupomona.edu
  • url http//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com