Java for Fucking Idiots: Learn the Basics of Java Programming Without any experience!



Download 3,91 Mb.
Pdf ko'rish
bet44/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   40   41   42   43   44   45   46   47   ...   70
Bog'liq
Java for Fucking Idiots

class
House {
String
color
;
House() {


color
=
"White"
;
}
House(String customColor) {
color
= customColor;
}
}
A class with two different ways to create an object from it!
Specify the color if you want. If you don’t, it will be White! Java is so smart
(even smarter than me) that it will use the constructor that matches
whatever you throw into the parentheses ().
Here’s how we’d provide Bob with the specifications for two different
houses. The first one we don’t give a shit what color he paints it. The
second one needs to be a beautiful magenta.
//Use the empty constructor
House houseOne =
new
House();
//Use the String customColor constructor
House houseTwo =
new
House(
"Magenta"
);
System.
out
.println(houseOne.
color
);
System.
out
.println(houseTwo.
color
);
Here’s the output.
> White 
> Magenta
The parentheses can be empty or have a value for the customColor
parameter!
This is a little bit more valuable when you have a real class more than one
property. But this book is designed for morons so what the hell do you want
from me? You can have more than one constructor. Lesson over.
null


Ever heard of a NullPointerException? No? Well if you take up Java, you’re
going to be seeing them for the rest of your life.
But before we get there, let’s talk about null . You can nullify an object like
you’d nullify your third marriage.
Marriage marriage =
null
;
That variable is set to null. What is null?
It’s a special, secret sauce. There’s nothing else like it in Java; it basically
means “the absence of anything.”
And here’s the unfortunate thing; with all these cool object-oriented
concepts we’ve learned: properties, methods, and the like, null was never
invited to the party. And it’s got a vengeance.
Null means your object has absolutely no value and if you try use its
properties or methods, there will be hell to pay. Meaning an application
crash if you’re not careful.
Let’s revive the House example for a second. We know it has a property
called 
color
. Why don’t’ we try to give that variable a value when the
House hasn’t even been built yet?
House stevesHouse =

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   40   41   42   43   44   45   46   47   ...   70




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