• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Object creation

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

code 1


code 2



what is the difference between these two codes
in code 2 : is the new arraylist object is created very time the loops run?.
in code 1 : only one arrauylist is created?.

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



Is the new arraylist object is created very time the loops run?.



Yes for both code 1 and 2
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In first case only one ArrayList object will be created in the heap.... while in second case 10 new ArrayList object will get created.....
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In first case only one ArrayList object will be created in the heap.... while in second case 10 new ArrayList object will get created.....




Do you mean reference handlers? Anyways, both coding styles shouldnt matter as the gc will take care of the short-lived references.
 
Kumari Priyanka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.....gc will take care of the short lived references.....but the first is the best practice that you should follow......
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumari Priyanka wrote:yes.....gc will take care of the short lived references.....but the first is the best practice that you should follow......



I disagree. If a variable is meant to be limited to a certain scope it shouldn't be declared outside of that scope, because it may then inadvertently escape its inteded scope.
If you minimize the scope of the variable this cannot happen and the variable will be more readily recognizable as a local variable to programmers that will have to maintain the code.
 
We can walk to school together. And we can both read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic