This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
I am confused regarding the lower case primitive types versus the capital case class primitive types (i.e. float, double, integer versus Float, Double, Integer). How can I distinguish when to use which? What kind of result does a class primitive type give & what does reference to it mean? Please clarify this for me!
Aha! Good question! And now to one answer. The phrase "class primitive" is incorrect. In Java everything is either an object of a class (like Integer or Panel) OR a primitive. Objects have functionality associated with them, and primitives don't. They just are. For example, int i=1; is a primitive. There is relatively little that it can do. I won't insult your intelligence by reminding you that a quantity like that can be added, subtracted, multiplied, shifted and I am fast running out of things it can do. They are all intuitive from our high school days. An Object is a different animal. Cutting to the chase, the Integer class can associate a number with an instance of that class for example, Integer i = new Integer(1); But i is now an object and not a primitive. For a complete repertoire of things that you can do with an Integer object, see Class Integer Sometimes making an Object represent a primitive may be necessary because there are classes with methods that accept only Objects and so primitives would have to be made Objects before being used by those methods. Hope this helps. BTW, these are the primitives: byte short char int long float double boolean Did I leave out any?
[This message has been edited by Tony Alicea (edited February 04, 2000).]
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Joined: Nov 22, 2008
Tony, Thank you for your detailed answer. It's quite helpful. Can you provide me with an example of an integer being used as an object?
Joined: Jan 30, 2000
An example from scratch: what if you needed to have the names of people associated with their age? The pair would be a person/age pair. So you could define some kind of "dictionary" that given the name of the person as a key (a String?), it would return the age of the person (an int). Such data structure already (almost, because of the parameters that the applicable methods expect) exists in Java and the interface defining it is the interface java.util.Map In particular, the method <CODE>public Object put(Object key, Object value)</CODE> requires Objects as parameters and that excludes primitives like <CODE>int</CODE>. Also, with the corresponding method <CODE>public Object get(Object key)</CODE> the 'thing' returned is an Object, NOT a primitive. So there, that would be a case of where you would need to convert an <CODE>int</CODE> primitive into an Integer object, and back - after <CODE>get</CODE>ting my age, for example. But for my age you'd really need a <CODE>long</CODE>! Ha ha! [This message has been edited by Tony Alicea (edited February 08, 2000).]
I used an integer as an object just recently. I have to describe the data found in a particular database table. For each column, there is a description and there might be some formatting for the results. In my object that dynamically describes how to display this option, there is String columnName ; String description ; Object format ; format is usually null, meaning that you just show the column data as is. Format can be two other things: 1) A format object that contains textual and clipping info 2) An integer representing a sting in the international text lookup (the value 52 would reference "@ degrees" in english (replace the @ with the database result) and "@ degrees, eh?" in Canadian or "@ efhsfft" in Elbonian. I couldn't store an int here, so I had to put the value into an Integer object.