I think reason why you cannot use == for conditions for your if to check your String is, String is an Array of chars, which means String can be char[] as well. so by doing if(check == "left"), you are trying to compare "left" with char[0], and char[0] is character 'l'. It's only what I thought btw. Additionally, if you call an array, without specifying it's location in the array, just like only using check, it will call the first element of that array.
@Mrgreatestfreakout Nope, vilham437 is right. The reason is that a string is an "object" and you can't compare objects with the == sign because with that you will compare the location of the objects, so when you compare two strings a & b with text "hello world" on both of them, you will get false because those are two objects at different locations, even when they have the same text in them.
DUUUUUDDEEE, LEARNING SOOO MUCH. Thank you! Have background in Java, but it's been awhile, and I want to get into Android programming. Been following your tut's, going to 200 baby! :D
@mastermax7777 There is a giant difference between = and ==. You're probably new, but = is the assignment operator to assign a variable something. And == is the comparing operator, to compare two things. .contentEquals should be the same as ==. So I've got the same question. And yes, also java uses ==.
@joannis11 Wrong. That will simply compare if they are initialized in the same way. Since String is a class and not a variable type, per se, you have to use a method to compare them.
@xXxDiukexXx you cannot use == to compare the contents of strings or any other objects because == with objects is checking to see if they have the same location in memory instead of checking to see if the strings contain the same word. The .contentEquals() method compares the actual strings instead of the memory location
if you compare primitives like int, long or float then == compares values
But if you do == on object variables (aka reference types) then it compares memory addresses.
It pretty much comes down to the inherently different nature of primitive types vs reference types(objects like String)
When you declare primitive variable it stores the value b/c they have fixed byte length. But objects have different sizes and you can't easily reserve memory so u store address instead
why can't you use equals() instead of contentEquals()? What's the difference... for the most part?
YourSovietComrade 1 month ago
Excelent Tutorial, ALL..
ARBOLEDA318 2 months ago
this is a string, for the most part.....HAHAHAHA... love it.
TheDomeOfGenesys 2 months ago in playlist Android App Development Tutorials (thenewboston)
Dude, we know Java already.
1arithehun 2 months ago
"Hey, if you need anyone to set up some buttons in Java .. I'm you're man!"
that's gonna be my new pickup line for sure.
DekenFrost 2 months ago in playlist Android Application Development Tutorials
'For the most part"
Everybody drink!
BYONDfps 3 months ago in playlist Android App Development Tutorials (thenewboston)
I tested it with variable.containts(" "); and it works as well as contentEquals(" ")
Antila 4 months ago
I'm going to kill myself if I hear "for the most part" again :(
zazzlescat 5 months ago in playlist TheNewBoston - Android
@zazzlescat let me help you... "for the most part"
petermihaylov 5 months ago
Is there any way to include an OR statement in? Eg. IF (condition1 OR condition2)?
Like how it works with adding an & to make a (condition1 & condition2) statement.
tehjow 5 months ago
@tehjow * if(x || y){} *
|| is the logical OR operator, similarly && is AND and ! is NOT.
MrShiftab 5 months ago
Yeah Cheer up for the most part.. :)))) jk, travis you're awesome! Thanks
specipful 6 months ago
I think reason why you cannot use == for conditions for your if to check your String is, String is an Array of chars, which means String can be char[] as well. so by doing if(check == "left"), you are trying to compare "left" with char[0], and char[0] is character 'l'. It's only what I thought btw. Additionally, if you call an array, without specifying it's location in the array, just like only using check, it will call the first element of that array.
Mrgreatestfreakout 6 months ago
@Mrgreatestfreakout Nope, vilham437 is right. The reason is that a string is an "object" and you can't compare objects with the == sign because with that you will compare the location of the objects, so when you compare two strings a & b with text "hello world" on both of them, you will get false because those are two objects at different locations, even when they have the same text in them.
xenoepist 6 months ago
what a great people gathered up here, cheer up guys!
Mrgreatestfreakout 6 months ago
DUUUUUDDEEE, LEARNING SOOO MUCH. Thank you! Have background in Java, but it's been awhile, and I want to get into Android programming. Been following your tut's, going to 200 baby! :D
snag46ed 6 months ago 3
This has been flagged as spam show
i have watched 27 tutorials nonstop today :d
bohterham 6 months ago in playlist Android MBB TNB
Comment removed
bohterham 6 months ago in playlist Android MBB TNB
errors are a good thing it will train you to fix somthing and fin the broken part
ginoPD 6 months ago
I really like these tutorials...for the most part.
kopilatis10 6 months ago in playlist TheNewBoston - Android 51
where is tutorial 27, anyone know? thanks
jakewestgomila 6 months ago
@jakewestgomila /playlist?list=PL33384E9848C4F55E
hoenoty 6 months ago in playlist Android Application Development Tutorials (thenewboston)
where's Tutorial 27?
simshelloworld 6 months ago
can't we use == instead of .contentEquals() ?
xXxDiukexXx 6 months ago
@xXxDiukexXx he explained that u cant in the tutorial..
this is java what do u expect =/
mastermax7777 6 months ago
Comment removed
joannis11 6 months ago
@mastermax7777 There is a giant difference between = and ==. You're probably new, but = is the assignment operator to assign a variable something. And == is the comparing operator, to compare two things. .contentEquals should be the same as ==. So I've got the same question. And yes, also java uses ==.
joannis11 6 months ago
@joannis11 new to java? maybe yes but im not an idiot :)
if he said you should use contentEquals then he said it for a reason.
mastermax7777 6 months ago
@joannis11 Wrong. That will simply compare if they are initialized in the same way. Since String is a class and not a variable type, per se, you have to use a method to compare them.
1arithehun 2 months ago
@xXxDiukexXx you cannot use == to compare the contents of strings or any other objects because == with objects is checking to see if they have the same location in memory instead of checking to see if the strings contain the same word. The .contentEquals() method compares the actual strings instead of the memory location
vilham437 6 months ago 25
@vilham437 Same memory location? Are you sure? I thought it checked if the reference was to the same _OBJECT_, I might be wrong though
noideayo 5 months ago
@noideayo he is right
if you compare primitives like int, long or float then == compares values
But if you do == on object variables (aka reference types) then it compares memory addresses.
It pretty much comes down to the inherently different nature of primitive types vs reference types(objects like String)
When you declare primitive variable it stores the value b/c they have fixed byte length. But objects have different sizes and you can't easily reserve memory so u store address instead
Mr1159pm 3 months ago in playlist More videos from thenewboston 2
@Mr1159pm I'be been coding for years and your comment made me understand primitives vs objects from a new perspective.
Brick10Man 1 month ago
@vilham437 This is true only for java. In c# or Python you can use == to compare Strings
supersushi269 3 months ago
@vilham437 Or .equals()
1arithehun 2 months ago
@vilham437
but if strings got the same contain, they just take the same place in memory
example, if u got 20 strings and "lol" in them, it just will create one String object
so you can check contain of the strings
devilazpl 1 month ago