— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet261/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   257   258   259   260   261   262   263   264   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

274
 
chapter 10
Math 
methods
Methods in the Math class 
don’t use any instance 
variable values. And because 
the methods are ‘static’, 
you don’t need to have an 
instance of Math. All you 
need is the Math class.
MATH methods: as close as you’ll 
ever get to a 
global method
Except there’s no global anything in Java. But think about 
this: what if you have a method whose behavior doesn’t 
depend on an instance variable value. Take the round() 
method in the Math class, for example. It does the same 
thing every time—rounds a floating point number(the 
argument to the method) to the nearest integer. Every 
time. If you had 10,000 instances of class Math, and ran 
the round(42.2) method, you’d get an integer value of 
42. Every time. In other words, the method acts on the 
argument, but is never affected by an instance variable 
state. The only value that changes the way the round() 
method runs is the argument passed to the method!
Doesn’t it seem like a waste of perfectly good heap space 
to make an instance of class Math simply to run the 
round() method? And what about other Math methods 
like min(), which takes two numerical primitives and 
returns the smaller of the two. Or max(). Or abs(), which 
returns the absolute value of a number. 
These methods never use instance variable values
. In fact the 
Math class doesn’t have any instance variables. So there’s 
nothing to be gained by making an instance of class 
Math. So guess what? You don’t have to. As a matter of 
fact, you can’t.
This error shows that the Math 
constructor is marked private
! That 
means you can NEVER say ‘new’ on the 
Math class to make a new Math object.
File Edit Window Help IwasToldThereWouldBeNoMath

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   257   258   259   260   261   262   263   264   ...   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