Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet234/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   230   231   232   233   234   235   236   237   ...   341
Bog'liq
Effective Java



CHAPTER 9
GENERAL PROGRAMMING
262
postpone the declaration until you do. One exception to this rule concerns 
try
-
catch
statements. If a variable is initialized to an expression whose evaluation can
throw a checked exception, the variable must be initialized inside a 
try
block
(unless the enclosing method can propagate the exception). If the value must be
used outside of the 
try
block, then it must be declared before the 
try
block,
where it cannot yet be “sensibly initialized.” For an example, see page 283.
Loops present a special opportunity to minimize the scope of variables. The
for
loop, in both its traditional and for-each forms, allows you to declare 
loop
variables
, limiting their scope to the exact region where they’re needed. (This
region consists of the body of the loop and the code in parentheses between the
for
keyword and the body.) Therefore, 
prefer 
for
 loops to 
while
 loops
, assum-
ing the contents of the loop variable aren’t needed after the loop terminates.
For example, here is the preferred idiom for iterating over a collection (Item 58):
// Preferred idiom for iterating over a collection or array
for (Element e : c) {
... // Do Something with e
}
If you need access to the iterator, perhaps to call its 
remove
method, the preferred
idiom uses a traditional 
for
loop in place of the for-each loop:
// Idiom for iterating when you need the iterator
for (Iterator i = c.iterator(); i.hasNext(); ) {
Element e = i.next();
... // Do something with e and i
}
To see why these 
for
loops are preferable to a 
while
loop, consider the following
code fragment, which contains two 
while
loops and one bug:
Iterator i = c.iterator();
while (i.hasNext()) {
doSomething(i.next());
}
...
Iterator i2 = c2.iterator();
while (

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   230   231   232   233   234   235   236   237   ...   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