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<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>
Joined: Oct 31, 2000
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
Joined: May 05, 2000
It would not be a major effort to write your own wrapper classes that are changeable.
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?
Joined: Nov 09, 2000
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.
Joined: Jan 31, 2001
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