• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Primitive Types

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, if necessary, a BigInteger.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim is CORRECT!
 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic