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



Download 3,91 Mb.
Pdf ko'rish
bet58/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   54   55   56   57   58   59   60   61   ...   70
Bog'liq
Java for Fucking Idiots

"Everybody Poops"
;
double
price =
2.99
;
String messageToUser =
"Thanks for purchasing the "
+ itemName +
" for $"
+ price +
"."
;


You might see “Thanks for purchasing Everybody Poops for $2.99.” when
we eventually show that String to the user. No, 
System.out.println()
doesn’t
cut it for a website…but that’s a different book.
Also, you bet your ass I added an extra String concatenation to put a period
at the end of my sentence, even for an example. Shit pisses me off.
Anyway, String concatenation is used eeeeeverywhere. I mean really.
Combining Strings with Strings, or with numbers, or objects, or all kinds of
shit. + is an important little dude.
Formatting
That last example was a lot of plus signs to make a big ol’ String. Those
variables really belonged inside the String there, didn’t they? We had to
manually build the String ourselves; cutting up the string into parts, and
inserting the variables where they should fit in.
There’s an easier way. It’s a static method available on the String class
called 
format
. Yes, it’s static, meaning you can literally call String.
format
(note that capital S) without needing an object to use the method.
Here’s how you might do the exact same message with this approach.
String messageToUser =
String.
format
(
"Thanks for purchasing %s for %f."
, itemName, price);
See those letters with a percentage sign % before them? No, it’s not a
percentage sign like you’re used to. This isn’t a value out of 100. This isn’t
the modulo operator either.
They’re like little placeholders. Then, when you use String.format, you give
Java the real values! It will insert them where they belong.
There is a method (hehe) to the madness of which letter to use. Basically,
each type of variable has its own letter. And you know what?
I’m not pasting a bit chart here. If you find yourself needing to make a
String that contains variables in it, use your Google-fu to 
find the table
online
 . Or just use %s to treat everything as a String. :)


Strings are Immutable
What does immutable mean? Does it describe your remote when you’re
watching TV and need to mute it, but it just won’t work?
No. It means Strings cannot be changed.
Concatenation? That created a new String.
Any time you use one of the String methods we’ve seen? They created new
Strings.
You need to make sure you’re not expecting the String to change by using
one these methods. Instead, you must expect Java to create a new String
from the shit you’re asking it do.
String initialString =

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   54   55   56   57   58   59   60   61   ...   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