In Java when an instance is created for a class, JVM loads the class, initialize the instance variables by calling the constructor and its super constructor. Is it because of this we say object creation is costly or is there any specific reason because why we say object creation is costly?
Object creation is actually not nearly so costly as it once was, either in Java or in other languages. The reason for this old chestnut is that chunks of memory for objects have to be handed out by a piece of system software called an allocator. The allocator has to keep track of what memory is available to be used for new objects, and it has to be able to accept newly-freed memory to add back into the allocatable pool. Because of these responsibilities, an allocator is fairly complex, and historically, they took a relatively long time to hand out a chunk of memory. This time is the "cost", plain and simple.
Today's allocators are much more sophisticated than they once were, and creating an object is not a very expensive operation anymore. In fact, in modern JVMs, many objects are actually secretly allocated on the machine stack, and that's basically free-- it takes no time at all.
Thanks for idea Ernest. My problem was not solved.But still when I am coding with java I am always cautious to create a new object if I really want a new object.And code reviewers always advice me not to create unnecessary objects as a best practice.According to your opinion are you saying that we do not need to be careful when creating new objects?. Wont it be a performance hit if we create many objects as we want?
Buddhika Mawella wrote:Wont it be a performance hit if we create many objects as we want?
In general, avoiding unnecessary object creation is generally a good idea, but the truth is that in recent JVMs creating small temporary objects -- especially wrapper objects like Integer or Double -- almost doesn't matter anymore. A lot of work has gone into making that kind of allocation essentially free.
Also, terms like "costly" and "cheap" are relative. You need to ask relative to what? And does it matter? Object creation may be 10 times slower than, say, adding ints, and 1000 times faster than reading a file. (I just made those numbers up; do not pay attention to the exact values, as they don't matter.) Does that mean we should never read a file? No, because reading a file is still pretty quick compared to many other things we humans do, like taking a breath of air or eating a donut. Likewise, creating objects is remarkably fast, and often it really has no discernable effect on your program's performance. Something else in the program is often much slower, yet still the program may be fast enough for the user that it just doesn't matter.
Put another way, avoiding unnecessary object creation may be a good idea if the object creation really is completely unnecessary. If nothing else, it's good practice for those comparatively rare times that it actually has an observable effect on performance. But don't work too hard to avoid creating objects that are actually useful to you in some way. That's what object-oriented programming is all about: we often find benefits in organizing our data and programs this way.