aspose file tools
The moose likes Java in General and the fly likes Primitives vs objects Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Primitives vs objects Doubt" Watch "Primitives vs objects Doubt" New topic
Author

Primitives vs objects Doubt

nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 38
Can anyone tell me advantages and disadvantages of using primitives and objects in an application. Like performance issues, memory management, handling errors and lot more. I started learning java since 1 and half month and right now I am working on J2EE. I have fair knowledge about C so I was using primitives most of the time while I was working with java but since I started working on with J2EE my coding became more object oriented. At times I feel am I loosing something with primitives.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I don't understand the question.

You practically never have a choice in front of you of using either primitives or objects for the same task, so the consideration of "advantages and disadvantages" of using one or the other hardly ever come up. Primitives are lower-level constructs with particular behavior -- numbers or small places to hold codes of some sort. Objects are higher-level, always inherit from java.lang.Object, always have Object's methods, etc.

We had a question in these forums recently on an example that stored a person's first and last name and the grade they made. I suppose it would be possible to implement that with primitives -- arrays of arrays of char, int for the grade, etc. -- but I can't imagine anyone who had seen a java tutorial or book attempting it.

So can you please elaborate on your question? "Losing something with primitives" doesn't explain it any better.

rc
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

Hi nithin chinni

Welcome to JavaRanch,

If you are using wrapper classes in case of primitive types most of time then it may be possible that you will run out of memory.


SCJP6.0,My blog Ranchers from Delhi
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

In general, using wrapper objects is less efficient for performance and memory use than using primitive types directly.

A wrapper object, for example an Integer, is just a small object that contains an int value. Primitive variables can be used in calculations directly, but when the primitive value is wrapped in an object, Java only has indirect access to the value: each time you use it in a calculation, the JVM has to get the value from the object.

Since autoboxing was added in Java 5, this overhead is not so clear, because it's not explicitly visible in the code what happens exactly. Let's look at a the following code:

What really happens here, with the help of autoboxing and -unboxing, is this:

You see that there are a lot of method calls there, which you would not have when you would be working directly with primitive int values. All those method calls make calculations with Integer wrapper objects a lot more work for the JVM than with primitive int values.

I've once tried out to find out how large a Double object was compared to a double primitive variable. It turned out that a Double uses 24 bytes on my JVM, while a double uses only 8 bytes. So if I'd want to store millions of double-precision floating-point variables in memory, using Double would make my program use three times as much memory as using double primitives.

Shanky Sohar wrote:If you are using wrapper classes in case of primitive types most of time then it may be possible that you will run out of memory.

But you can run out of memory as well when using primitive values. It's not as if it is impossible to run out of memory when you use primitives.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

I mean chances are high.when you use wrapper classes..
nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 38
thanks guys for your help
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Primitives vs objects Doubt
 
Similar Threads
How is the job market in Australia?
How Java is pure object oriented language?
String and StringBuffer
Why primitive types don't need a new keyword for creation?
Any one using Eclipse for real development?