— Scott McNealy, Sun Microsystems Chairman, President and ceo


int giveNumber() {  return x; } if ( bool



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

int
giveNumber() { 
return x;
}
if (
bool
) {
System.out.println(“true”);
}
boolean
true
boole
an obje
ct
true


292
 
chapter 10
But wait! There’s more! Wrappers
have static utility methods too!
Besides acting like a normal class, the wrappers have a 
bunch of really useful static methods. We’ve used one in 
this book before—Integer.parseInt().
The parse methods take a String and give you back a 
primitive value. 
String s = “2”;
int x = Integer.parseInt(s);
double d = Double.parseDouble(“420.24”);
boolean b = new Boolean(“true”).booleanValue();
Converting a String to a 
primitive value is easy:
No problem to parse 
“2” into 2.
Uh-oh. This compiles just fine, but 
at runtime it blows up. Anything 
that can’t be parsed as a number 
will cause a NumberFormatException
File Edit Window Help Clue
% java Wrappers
Exception in thread “main” 
java.lang.NumberFormatException: two
at java.lang.Integer.parseInt(Integer.java:409)
at java.lang.Integer.parseInt(Integer.java:458)
at Wrappers.main(Wrappers.java:9)
String t = “two”;
int y = Integer.parseInt(t);
But if you try to do this:
You’ll get a runtime exception:
You’d think there would be a 
Boolean.parseBoolean() wouldn’t 
you? But there 
isn’t. Fortunately there’s 
a Boolean constructor 
that takes (and parses) a String, and then you 
just get the primitive value by unwrapping it.
Every method or 
constructor that parses 
a String can throw a 
NumberFormatException. 
It’s a runtime exception, 
so you don’t have to 
handle or declare it.
But you might want to.

Download 40,57 Mb.

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