— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

numbers 
and
statics
you are here
4
291
i++
;
int
3
Integ
er obj
ec

3
Double d = 
x
;
int
3
Integ
er obj
ec

3
Operations on numbers
This is probably the strangest one—yes, you 
can now use a wrapper type as an operand 
in operations where the primitive type is 
expected. That means you can apply, say, 
the increment operator against a reference 
to an Integer object!
But don’t worry—this is just a compiler trick. 
The language wasn’t modified to make the 
operators work on objects; the compiler 
simply converts the object to its primitive 
type before the operation. It sure looks 
weird, though.
Integer i = new Integer(42); 
i++;
And that means you can also do things like:
Integer j = new Integer(5); 
Integer k = j + 3;
Assignments
You can assign either a wrapper or primitive 
to a variable declared as a matching wrapper 
or primitive. For example, a primitive int 
variable can be assigned to an Integer 
reference variable, and vice-versa—a 
reference to an Integer object can be 
assigned to a variable declared as an int 
primitive.
Sharpen your pencil
public class TestBox {
Integer i;
int j;
public static void main (String[] args) {
TestBox t = new TestBox();
t.go();
}
 
public void go() {
j=i;
System.out.println(j);
System.out.println(i);
}
}
Will this code compile? Will it run? If it runs, 
what will it do?
Take your time and think about this one; it 
brings up an implication of autoboxing that 
we didn’t talk about. 
You’ll have to go to your compiler to find 
the answers. (Yes, we’re forcing you to 
experiment, for your own good of course.)

Download 40,57 Mb.

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