type wrappers in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sn20063757
    New Member
    • Aug 2007
    • 3

    #1

    type wrappers in java

    what is meant by the term type wrappers .And what is their us in java programming .
    For example
    import java.util.*;
    import javax.swing.JOp tionPane;
    public class Numyears
    {
    public static void main(String[]args)
    {
    GregorianCalend ar current=new GregorianCalend ar();
    int currentYear;
    int birthYear;
    int numYears;
    birthYear=Integ er.parseInt(JOp tionPane.showIn putDialog
    (null,"in what year were you born?",JOptionP ane.QUESTION_ME SSAGE));
    currentYear=cur rent.get(Calend ar.YEAR);
    numYears=curren tYear-birthYear;
    JOptionPane.sho wMessageDialog( null,"you will turn"+numYears+ "years old",JOptionPan e.INFORMATION_M ESSAGE);
    system.exit(0);
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Pardon my blindness but I don't see any wrappers in your code.

    kind regards,

    Jos

    ps. for a good wrapper/decorator example see all the InputStream classes.

    Comment

    • etiainen
      New Member
      • Aug 2007
      • 40

      #3
      Yeah, you didn't use any wrappers in your example but still...
      They can be used to make objects out of your primitives. This is useful if you are using some non generic collections (those without <T>) or if you in any way intend to pass your primitive value to a method expecting an object.

      example:

      Code:
      //...
      int somePrimitive=1;
      java.util.ArrayList aList=new java.util.ArrayList();
      
      //the right way:
      aList.add(new Integer(somePrimitive));
      
      //this would cause an exception:
      //aList.add(somePrimitive);
      //...
      ou yeah, AND they can be used as mentioned in previous post to wrap the functionality of a more general type, as in:

      Code:
      DataInputStream dis=new DataInputStream(new FileInputStream("someFile.txt"));
      Last edited by etiainen; Aug 15 '07, 12:00 AM. Reason: forgot something

      Comment

      Working...