Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive Data Types

 
Bala jee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one expain me what is Primitive Data Types and the difference between Primitive and Wrapper Classes?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 35
  • Mark post as helpful
  • send pies
  • 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?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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?
 
satish murali
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic