• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the importance of wrappers

 
Amit Badle
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Art Metzer
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would not be a major effort to write your own wrapper classes that are changeable.

 
Amit Badle
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic