Effective Java


// try-with-resources with a catch clause



Download 2,19 Mb.
Pdf ko'rish
bet43/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   39   40   41   42   43   44   45   46   ...   341
Bog'liq
Effective Java

// try-with-resources with a catch clause
static String firstLineOfFile(String path, String defaultVal) {
try (BufferedReader br = new BufferedReader(
new FileReader(path))) {
return br.readLine();
} catch (IOException e) {
return defaultVal;
}
}
The lesson is clear: Always use 
try
-with-resources in preference to 
try
-
finally
when working with resources that must be closed. The resulting code is
shorter and clearer, and the exceptions that it generates are more useful. The 
try
-
with-resources statement makes it easy to write correct code using resources that
must be closed, which was practically impossible using 
try
-
finally
.


37
C H A P T E R
3
Methods Common to All Objects
A
LTHOUGH
Object
is a concrete class, it is designed primarily for extension.
All of its nonfinal methods (
equals

hashCode

toString

clone
, and 
finalize
)
have explicit 
general contracts
because they are designed to be overridden. It is
the responsibility of any class overriding these methods to obey their general con-
tracts; failure to do so will prevent other classes that depend on the contracts (such
as 
HashMap
and 
HashSet
) from functioning properly in conjunction with the class.
This chapter tells you when and how to override the nonfinal 
Object
methods.
The 
finalize
method is omitted from this chapter because it was discussed in
Item 8. While not an 
Object
method, 
Comparable.compareTo
is discussed in this
chapter because it has a similar character.
Item 10: Obey the general contract when overriding 
equals
Overriding the 
equals
method seems simple, but there are many ways to get it
wrong, and consequences can be dire. The easiest way to avoid problems is not to
override the 
equals
method, in which case each instance of the class is equal only
to itself. This is the right thing to do if any of the following conditions apply:


Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   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