Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is mean by creating some object will be expensive in java/oop

Tags:

java

object

I heard this statement many times when reading some java books/articles.My question is very straightforward that when we say that creating some object will be very expensive?

Here expensive is for what and at what scenario we should use this term.It would be very easy for me to understand if some one illustrate with small example and how to avoid this?

like image 441
Prashant Shilimkar Avatar asked Dec 04 '13 03:12

Prashant Shilimkar


People also ask

Is creating objects expensive Java?

Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code. Making a class field public can cause lot of issues in a program.

Why is object creation expensive?

Object creation is considered as costly because it consumes your memory.As a result of this, program will suffer from lack of resources(memory) and it become slow.

What does it mean to create an object in Java?

Creating an Object So basically, an object is created from a class. In Java, the new keyword is used to create new objects. There are three steps when creating an object from a class − Declaration − A variable declaration with a variable name with an object type.

What is the benefit of creating an object in Java?

Objects are required in OOPs because they can be created to call a non-static function which are not present inside the Main Method but present inside the Class and also provide the name to the space which is being used to store the data.


1 Answers

Expensive usually means it'll take a while, but it can also mean it'll take a lot of some other resource, such as memory, bandwidth, hosting budget, disk space, or anything else you'd like to use less of. For example,

new int[1000000000]

will be expensive, because it allocates and zeros an incredible amount of memory.

like image 133
user2357112 supports Monica Avatar answered Nov 03 '22 10:11

user2357112 supports Monica