— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

ArrayList listOfNumbers = new ArrayList();

listOfNumbers.add(new Integer(3));
Integer one = (Integer) listOfNumbers.get(0);
int intOne = one.intValue();
}
Without autoboxing (Java versions before 5.0)
Make an ArrayList. (Remember, before 5.0 you could not 
specify the TYPE, so all ArrayLists were lists of Objects.)
You can’t add the primitive ‘3’ to the list, 
so you have to wrap it in an Integer first.
It comes out as type 
Object, but you can cast 
the Object to an Integer.
Finally you can get the primitive 
out of the Integer.


numbers 
and
statics
you are here
4
289
public void doNumsNewWay() {
ArrayList

listOfNumbers = new ArrayList

();
listOfNumbers.
add(3)
;
int num = listOfNumbers.get(0);
}
With autoboxing (Java versions 5.0 or greater)
Make an ArrayList of type Integer.
Just add it!
Although there is NOT a method in ArrayList 
for add(int), the compiler does all the wrapping 
(boxing) for you. In other words, there really IS 
an Integer object stored in the ArrayList, but 
you get to “pretend” that the ArrayList takes 
ints. (You can add both ints and Integers to an 
ArrayList.)
And the compiler automatically unwraps (unboxes) 
the Integer object so you can assign the int value 
directly to a primitive without having to call the 
intValue() method on the Integer object.
Autoboxing: blurring the line 
bet ween primitive and object
The autoboxing feature added to Java 5.0 does 
the conversion from primitive to wrapper object 
automatically!
Let’s see what happens when we want to make an 
ArrayList to hold ints. 
An ArrayList of primitive ints
Q: 

Download 40,57 Mb.

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