This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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>
Art Metzer
Ranch Hand
Joined: Oct 31, 2000
Posts: 241
posted
0
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
posted
0
It would not be a major effort to write your own wrapper classes that are changeable.
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.
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.