• 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

Primitives vs objects Doubt

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean chances are high.when you use wrapper classes..
 
nithin chinni
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys for your help
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic