Effective Java


Item 6: Avoid creating unnecessary objects



Download 2,19 Mb.
Pdf ko'rish
bet31/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   27   28   29   30   31   32   33   34   ...   341
Bog'liq
Effective Java

Item 6:
Avoid creating unnecessary objects
It is often appropriate to reuse a single object instead of creating a new function-
ally equivalent object each time it is needed. Reuse can be both faster and more
stylish. An object can always be reused if it is immutable (Item 17).
As an extreme example of what not to do, consider this statement:
String s = new String("bikini");
// DON'T DO THIS!
The statement creates a new 
String
instance each time it is executed, and none
of those object creations is necessary. The argument to the 
String
constructor
(
"bikini"
) is itself a 
String
instance, functionally identical to all of the objects
created by the constructor. If this usage occurs in a loop or in a frequently invoked
method, millions of 
String
instances can be created needlessly.
The improved version is simply the following:
String s = "bikini";
This version uses a single 
String
instance, rather than creating a new one
each time it is executed. Furthermore, it is guaranteed that the object will be
reused by any other code running in the same virtual machine that happens to
contain the same string literal [JLS, 3.10.5].
You can often avoid creating unnecessary objects by using 
static factory meth-
ods 
(Item 1) in preference to constructors on immutable classes that provide both.
For example, the factory method 
Boolean.valueOf(String)
is preferable to the
constructor 
Boolean(String)
, which was deprecated in Java 9. The constructor
must
create a new object each time it’s called, while the factory method is never
required to do so and won’t in practice. In addition to reusing immutable objects,
you can also reuse mutable objects if you know they won’t be modified.
Some object creations are much more expensive than others. If you’re going
to need such an “expensive object” repeatedly, it may be advisable to cache it for
reuse. Unfortunately, it’s not always obvious when you’re creating such an object.
Suppose you want to write a method to determine whether a string is a valid
Roman numeral. Here’s the easiest way to do this using a regular expression:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish