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



Download 3,91 Mb.
Pdf ko'rish
bet53/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   49   50   51   52   53   54   55   56   ...   70
Bog'liq
Java for Fucking Idiots

boolean
isSeven(
int
number) {
return
number
==
7
;
}
System.
out
.println(isSeven(
5
));
We print out false because 5 ==7 is simply not true.
Strings, though, should not be compared like that. Bad! Bad comparison
operator!


Let me show you why. For the purposes of this example, I will be using the
String constructor instead of just some double quotes. Please, forgive me.
String strOne =
new
String(
"Ayy lmao."
);
String strTwo =
new
String(
"Ayy lmao."
);
boolean
sameString = strOne == strTwo;
You’re on Who Wants to be a Millionaire? What is the value of 
sameString
? Just like the show, you can phone a friend if you’d like.
A. true
B. false
C. null
D. The program crashes
First of all, a primitive cannot be 
null
, so jot that down.
But the answer is B: 
false
! Yep, those strings are not the same according to
the comparison operator (double equals sign).
We know they’re the same, however. So what’s the deal, why is it saying
they’re not the same?
When it comes to objects, the comparison operator == is literally checking
“are these the same object?”
Well, these aren’t. You created two different objects, right? 
strOne
and
strTwo
. You failed.
The lesson? Don’t use the comparison operator for Strings, or any object for
that matter, if you’re trying to see if they’re the same value.
How do we do it, then? Like this!
boolean
sameString = strOne
.equals
(strTwo);
Bam! 
sameString
is now true.
Superpowers
Alright, maybe not superpowers, per se. But Strings are objects, as I said.
You just saw the 
equals
method that checks if two Strings have the same


value.
Unlike the trite and overly simplified examples we’ve used so far, the
String’s superpowers are actually useful methods that accomplish actual
things! Let’s check out some of them!!
equals
We just covered it, but just to reiterate: when you want to compare two
strings, never use ==. Always use the 
equals
method that all Strings have.
boolean
sameString = strOne.equals(strTwo);
This is something you actually do a lot in Java. Did the user type in a
certain word or sentence? Does it match a value we expected? Now we can
do that with Strings!
Thanks Java!@! Programming is fun.
equalsignorecase
For when you want to use 
equals
but don’t care if the characters in the
string are uppercase (A) or lowercase (a).
//true

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   49   50   51   52   53   54   55   56   ...   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