— Scott McNealy, Sun Microsystems Chairman, President and ceo


int x = 32; ArrayList list = new ArrayList()



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

int x = 32;
ArrayList list = new ArrayList();
list.add(x);
This won’t work unless you’re using Java 5.0 or 
greater!! There’s no add(int) method in ArrayList 
that takes an int! (ArrayList only has add() methods 
that take object references, not primitives.)
wrapping a value
int i = 288;
Integer iWrap = new Integer(i);
Boolean
Character
Byte
Short
Integer
Long
Float
Double
There’s a wrapper class for every primitive type, 
and since the wrapper classes are in the java.
lang package, you don’t need to import them. 
You can recognize wrapper classes because 
each one is named after the primitive type it 
wraps, but with the first letter capitalized to 
follow the class naming convention.
Oh yeah, for reasons absolutely nobody on the 
planet is certain of, the API designers decided 
not to map the names exactly from primitive 
type to class type. You’ll see what we mean:
unwrapping a value
int unWrapped = iWrap.intValue();
Give the primitive to the 
wrapper constructor. That’s it.
All the wrappers work 
like this. Boolean has a 
booleanValue(), Character 
has a charValue(), etc.
Watch out! The names aren’t 
mapped exactly to the primitive 
types. The class names are fully 
spelled out.
Inte
g
er 
o
b
j
ec

int
int primitive
Integer object
Note: the picture at the top is a chocolate in a foil wrapper. Get 
it? Wrapper? Some people think it looks like a baked potato, but 
that works too. 


static 
methods
288
 
chapter 10
Before Java 5.0, YOU had to do the work...
She’s right. In all versions of Java prior to 5.0, primitives were primitives 
and object references were object references, and they were NEVER 
treated interchangeably. It was always up to you, the programmer, to do 
the wrapping and unwrapping. There was no way to pass a primitive to a 
method expecting an object reference, and no way to assign the result of a 
method returning an object reference directly to a primitive variable—even 
when the returned reference is to an Integer and the primitive variable is 
an int. There was simply no relationship between an Integer and an int, 
other than the fact that Integer has an instance variable of type int (to hold 
the primitive the Integer wraps). All the work was up to you.
This is stupid. You mean I can’t 
just make an ArrayList of ints??? I 
have to wrap every single frickin’ one in a new 
Integer object, then unwrap it when I try 
to access that value in the ArrayList? 
That’s a waste of time and an error 
waiting to happen...
An ArrayList of primitive ints
public void doNumsOldWay() {

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   270   271   272   273   274   275   276   277   ...   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