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 Data 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 Data Types" Watch "Primitive Data Types" New topic

Primitive Data Types

Bala jee
Ranch Hand

Joined: Sep 22, 2007
Posts: 35
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

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Bala jee
Ranch Hand

Joined: Sep 22, 2007
Posts: 35
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

Joined: Jan 29, 2003
Posts: 8791
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?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
satish murali

Joined: Sep 28, 2007
Posts: 1
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

Joined: Aug 31, 2004
Posts: 11343

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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Primitive Data Types
It's not a secret anymore!