• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Difference in results for JDK 1.4 And 1.5

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I ran this program in both JDK1.4 and JDK1.5

In JDK1.4 I got an error.
Incompatible type.
Found int required java.lang.Integer.

However it ran with out errors in JDK1.5.

Can some one tell me the difference.
I thought both of them had wrapper classes.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, wrapper classes exist in both Java 1.4 and Java 5, but autoboxing is a new feature of Java 5.

This does not compile on Java 1.4:

Integer i = 1000;

For Java 1.4, you'll have to write:

Integer i = Integer.valueOf(1000);
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
I think I still have doubts on that, but will study and let you know if there is anything I need to understand.Still weak on the concepts of Autoboxing.

Take Care.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question..

When i declare an instance of a wrapper class, does it creat an object on the heap?
And if yes, how is the object being created with out using "new".


Eg.

Integer i2=177;

I know that int value 177 is being Autoboxed to Integer type.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, whether created with "new" or with autoboxing, an Integer instance is an object on the heap. You don't need to say "new" because autoboxing automatically takes care of instantiating the object for you.

But one important difference is that autoboxing will not always create a new object. If the value is within the range of a byte (-128 to 127), then the boxed instances are "pooled." If the "pool" already contains a wrapped instance of that value, then autoboxing will return a reference to the pooled instance instead of creating a new object.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Can you please clear me about the concept of pooliing.
From what I gathered, Pooling is used to save memory and instead of creating a new object it looks for an object with the same value and creates a reference to it instead of creating a new one.
But what happens if there are no objects of that kind on the heap?
And what exactly do you mean by having and object on the pool.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic