— Scott McNealy, Sun Microsystems Chairman, President and ceo


Why not declare an ArrayList if you want to



Download 40,57 Mb.
Pdf ko'rish
bet276/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   272   273   274   275   276   277   278   279   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

Why not declare an ArrayList if you want to 
hold ints?
A: 
Because... you can’t. Remember, the rule for generic 
types is that you can specify only class or interface types, not 
primitives. So ArrayList will not compile. But as you can 
see from the code above, it doesn’t really matter, since the 
compiler lets you put ints into the ArrayList. In fact, 
there’s really no way to prevent you from putting primitives 
into an ArrayList where the type of the list is the type of that 
primitive’s wrapper, if you’re using a Java 5.0-compliant com-
piler, since autoboxing will happen automatically. So, you can 
put boolean primitives in an ArrayList and chars 
into an ArrayList.


static 
methods
290
 
chapter 10
void takeNumber(
Integer i
) { }
int
3
Integ
er obj
ec

3
int
giveNumber() { 
return x;
}
int
3
Integ
er obj
ec

3
if (
bool
) {
System.out.println(“true”);
}
boolean
true
boole
an obje
ct
true
Autoboxing works almost everywhere
Autoboxing lets you do more than just the obvious wrapping and
unwrapping to use primitives in a collection... it also lets you use 
either a primitive or its wrapper type virtually anywhere one or the 
other is expected. Think about that!
Fun with autoboxing
Method arguments
If a method takes a wrapper type, you 
can pass a reference to a wrapper or 
a primitive of the matching type. And 
of course the reverse is true—if a 
method takes a primitive, you can 
pass in either a compatible primitive 
or a reference to a wrapper of that 
primitive type.
Return values
If a method declares a primitive 
return type, you can return either a 
compatible primitive or a reference 
to the wrapper of that primitive type. 
And if a method declares a wrapper 
return type, you can return either a 
reference to the wrapper type or a 
primitive of the matching type.
Boolean expressions
Any place a boolean value is expected, 
you can use either an expression that 
evaluates to a boolean (4 > 2), or a 
primitive boolean, or a reference to a 
Boolean wrapper.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   272   273   274   275   276   277   278   279   ...   555




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