— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

static and 
final


numbers 
and
statics
you are here
4
285
Sharpen your pencil
What’s Legal?
Given everything you’ve just 
learned about static and final, 
which of these would compile?
1
public class Foo {
static int x;
public void go() {
System.out.println(x);
}
}
public class Foo2 {
int x;
public static void go() {
System.out.println(x);
}
}
public class Foo3 {
final int x;
public void go() {
System.out.println(x);
}
}
public class Foo4 {
static final int x = 12;
public void go() {
System.out.println(x);
}
}
public class Foo5 {
static final int x = 12;
public void go(final int x) {
System.out.println(x);
}
}
public class Foo6 {
int x = 12;
public static void go(final int x) {
System.out.println(x);
}
}
2
3
4
5
6


286
 
chapter 10
Math methods
Now that we know how static 
methods work, let’s look 
at some static methods in 
class Math. This isn’t all of 
them, just the highlights. 
Check your API for the rest 
including sqrt(), tan(), ceil(), 
floor(), and asin().
Math.random()
Returns a double between 0.0 through (but 
not including) 1.0.
double r1 = Math.random();
int r2 = (int) (Math.random() * 5);
Math.abs()
Returns a double that is the absolute value of 
the argument. The method is overloaded, so 
if you pass it an int it returns an int. Pass it a 
double it returns a double.
int x = Math.abs(-240); // returns 240
double d = Math.abs(240.45); // returns 240.45
Math.round()
Returns an int or a long (depending on 
whether the argument is a float or a double) 
rounded to the nearest integer value. 
int x = Math.round(-24.8f); // returns -25
int y = Math.round(24.45f); // returns 24
Remember, floating point literals are assumed 
to be doubles unless you add the ‘f’.
Math.min()
Returns a value that is the minimum of the 
two arguments. The method is overloaded to 
take ints, longs, floats, or doubles.
int x = Math.min(24,240); // returns 24
double y = Math.min(90876.5, 90876.49); // returns 90876.49
Math.max()
Returns a value that is the maximum of the 
two arguments. The method is overloaded to 
take ints, longs, floats, or doubles.
int x = Math.max(24,240); // returns 240
double y = Math.max(90876.5, 90876.49); // returns 90876.5
Math 
methods


numbers 
and
statics
you are here
4
287
object
primitive
Wrapping a primitive
Sometimes you want to treat a primitive like 
an object. For example, in all versions of Java 
prior to 5.0, you cannot put a primitive directly 
into a collection like ArrayList or HashMap:
When you need to treat 
a primitive like an object, 
wrap it. If you’re using any 
version of Java before 5.0, 
you’ll do this when you 
need to store a primitive 
value inside a collection like 
ArrayList or HashMap.

Download 40,57 Mb.

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