File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Primitive Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Primitive Types" Watch "Primitive Types" New topic

Primitive Types

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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!
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
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:
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
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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?
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
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).]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Or, if necessary, a BigInteger.

"I'm not back." - Bill Harding, Twister
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
paul wheaton

Joined: Dec 14, 1998
Posts: 20973

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.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
I agree. Here's the link:
subject: Primitive Types
It's not a secret anymore!