aspose file tools*
The moose likes Beginning Java and the fly likes the importance of wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the importance of wrappers" Watch "the importance of wrappers" New topic
Author

the importance of wrappers

Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi ,
the wrapper classes in java have their own importance in extending the functionality of primitives in the OOPs environment . but cud any1 give me a few practical exapmles where these wrappers r used.
amit
Fred Hosch
Author
Greenhorn

Joined: Apr 16, 2001
Posts: 23
not sure if this is what you're looking for, but java.io for
instance has many examples. BufferedReader wraps (and extends)
Reader, InputStreamreader wraps InputStream, etc.
------------------
Fred Hosch
Author of:
An Introduction to Software Construction with Java


Fred Hosch<br />Author of:<br /><a href="http://www.amazon.com/exec/obidos/ASIN/0471354899/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">An Introduction to Software Construction with Java</a>
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Say you have an applet on which you'd like to display the value of an int. But the arguments to g.drawString() are a String followed by the coordinates at which you'd like to place it. One of the ways of converting an int to a String is to use this static method from the wrapper class java.lang.Integer:

You also use static methods of this wrapper class to convert a String back to an int or to convert an int to its binary representation.
But wait, there's more.
Wrappers really enter in when you need to store primitive values as objects, for instance, when using vectors. You cannot say

but you can say

Keep in mind that wrapper classes' wrapped values are immutable: once you set 'em, you can't change 'em.
Hope this helps, Amit.
Art
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It would not be a major effort to write your own wrapper classes that are changeable.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi,
thanx for writing bout wrappers Art that was a pretty clear explanation. but some qs do pop pertaining to the structure of these wrappers and the overhead if any that comes along with them.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Well, for the structure of the Integer class, open your API and click java.lang, and then select Integer. It shows you all the methods and fields of this class.

As to overhead, there is always overhead involved when you instantiate classes. I'm not familiar with it, but perhaps there are certain JVM optimizations that make Integers not quite so bad as other regular classes. Or maybe it's the other way around. Maybe Java integers are worse than (for example) a C++ int.

Anyone looked under the hood?
Magesh Lakshmi
Greenhorn

Joined: Nov 09, 2000
Posts: 7
This is one point where the distinction between procedural language and Object oriented language lies. Consider a situation you want to convert an integer value to a string.
In procedural language like C, there would be some utility functions that ( oops I forgot C...long time ) can accept an int and convert into string, but note, these utility functions doesn't belong to any particular glue code ( in oops terminology it does not belong to any object ).
In OO language like Java, you can't write general utility functions which does not belong to any object.
So here in java, these utility functions are written at object levels.
The conclusion could be ,The wrapper over primitives are provided
1. Its a OO design paradigm
2. Extending the responsibility of primitives
3. Avoiding common utility functions that doesn't belong to
any class or objects.
Hope this helps.
Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi all,
thanx 2 every1 who replied 2 this post the explanations have further extended the expanse of my knowledge. such things encourage ppl like us 2 keep on posting.
Amit
 
Consider Paul's rocket mass heater.
 
subject: the importance of wrappers