• 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 Data Types

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one expain me what is Primitive Data Types and the difference between Primitive and Wrapper Classes?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two kinds of variable types in Java: primitive types and reference types.

The primitive types are: byte, short, int, long, char, float, double, boolean
See The Java Tutorial: Primitive Types

For each primitive type, there is a corresponding class in the Java API: Byte, Short, Integer, Long, Character, Float, Double, Boolean.

Note that in Java, primitive types are not objects. But sometimes you need an object; for example, the collection classes like ArrayList can only store objects, not primitive types. If you want to store integers in an ArrayList, you need to put those integers in objects, because you can't store ints directly into the ArrayList. That's what you use the Integer wrapper class for: to create an object to contain the int.
 
Bala jee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your info but still i want to know if both wrapper and primitive will be stored in the same memory?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. A primitive variable contains real data, just a few bytes of the value. For example, an int is four bytes that directly translate to the number you put there. Every primitive gets its own memory space.

An object reference variable - referring to one of the wrapper classes or any other object - contains a "pointer" to an object. It's not exactly like a C pointer, but it does roughly the same thing. The wrapper objects probably have a private instance of their primitives, and may have other data, such as numeric precision. Any number of variables can reference the same object.

Does that help, or just raise more questions?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to invoke the method of wrapper classes.

did the precsion of float and double will change in the wrapper.if you explain by an example it will be more helpful
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by satish murali:
...did the precsion of float and double will change in the wrapper...


Welcome to JavaRanch!

Precision of floating-point values is another issue, outlined in Some things you should know about floating-point arithmetic.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic