This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Primitive Data Types" Watch "Primitive Data Types" New topic
Author

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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
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
Greenhorn

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
Sheriff

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
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive Data Types
 
Similar Threads
Difference between Primitive & non-primitive data types?
Pass by value & pass by reference data types
oops concepts #2
type casting
Please brief on JavaScript architecture