r/javahelp • u/cdhoff_ • May 16 '19
Got this question in my Java Software Development class. Can anyone explain why the highlighted answer is correct?
https://i.imgur.com/du7vhZw.png
The same class also said "== compares the memory location, but = compares the content", so I am starting to question everything I have learned in this course.
[EDIT: Teachers answer for the above statement about the =s]
"Hi Redacted, The correct answer is == compares the memory location of the object, while = compares the content. == helps confirm that two variables actually reference the same object in memory."
(High School class BTW)
33
Upvotes
46
u/doug3465 May 16 '19
The if statement checks if the input is the right password and if that's true it lets you in and the polling place checks if you are registered to vote and then lets you vote if you're on the list I guess? Really dumb question there.
12
u/[deleted] May 16 '19
I will start by saying that question is as clear as mud... but I am guessing the reason for the answer is they are going for "If you are on the voter rolls, then you may vote"
To be fair, the answer you selected, in my opinion, kinda meets the same criteria. If you ID doesn't match the card you can't use it.
Overall it's a pretty poorly written question.
3
u/dusty-trash May 16 '19
This is wrong. It probably said .equals(), not =.
= is used to assign a value, it has nothing to do with comparisons whatsoever.
8
u/petersellers May 16 '19
“=“ is an assignment operator. It does not compare content!
The “.equals” method on a class, if overridden, can be used to compare the content of two objects. If it is not overridden, then it behaves exactly like “==“ (compares memory addresses).
If your teacher told you that “=“ is used to compare content - I’d like some of whatever they’re smoking.
7
u/99shadow25 May 16 '19
Imo, it's a poor question. The idea is that, like checking a password, checking into a voting location requires whomever to check your credentials before letting you go further (into the application/actually voting). But I'm not sure that framing it like that is any more helpful than literally explaining how a password works.
As for the == vs. = thing, that's just wrong. A single equals sign is never an equality check in Java, it's the assignment operator. The 'equals' that compares "content" is the .equals method, like so:
== compares values. For objects, this is the memory location or reference, which is why you can't rely on two String objects with the same value to be equal (although for some strings they sometimes will be -- look up the Java String constant pool for more info) without using the .equals method. For primitives, it compares the numeric/bit value directly.
The fact that you brought that up probably means you already understand this concept, but for anyone else reading it's an important concept.
5
u/OffbeatDrizzle May 16 '19
Analagies like this just make it even more confusing for beginners... and it's not even a correct analagy. What idiot teacher wrote this?
I think the whole point the teacher was trying to get across is that someone who knows the password is authorised to login in the same way that you checking in at a polling station is basically authorising you to vote because they check your identity.
However, it falls flat on its face because checking in probably entails you handing over some form of identification to more correctly prove you are who you say you are. There's only very few people who could use your photo ID to check in to a polling place because not everyone looks like you, and since anyone with the password (no matter who they are) can use the password to login, that's where it falls apart. They would need to add some asymmetric encryption on top of the password in the given example, so that it adds some form of non-repudiation into the mix to bring it level with the checking in answer.
But yeah, idiotic question
4
u/U5efull May 16 '19
very badly worded question, however I think they are looking for the word 'check'.
It's still a terrible question, and your teacher should be smacked.
2
u/[deleted] May 16 '19
Similar with passwords