r/javahelp • • 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

27 comments sorted by

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.

17

u/rossk14 May 16 '19

I can only assume this was something discussed in the class? This reasoning seems right, but yeah without context the question seems pretty dumb.

2

u/cdhoff_ May 16 '19

The thing about the logic you and rtc_op is that would be an example of an if-else statement I think.

If you register, you can vote
Else, you can't vote.

2

u/Ariakkas10 May 16 '19

Think of "else" as another if.

If you are registered, you can vote, else if your friend is registered, you wait for them to finish.

Else isn't just what happened if the "if" fails, it's for when the "if" fails and you need to check more stuff.

1

u/cdhoff_ May 16 '19

Thanks, that helps 😊

1

u/ThatLesbian Extreme Brewer May 16 '19

Else is exactly for what you do when the if fails. Else if is for when the if fails and you need to check more stuff.

6

u/doug3465 May 16 '19

Technically I think "if statement" could encompass that and that is still just a binary registered or not, like the password check (correct password or not). Also, I don't really see the case for the credit card answer :/

5

u/Shaddaa May 16 '19

I wouldn't say this is an if-else statement. Because following your logic, every if statement is an if-else statement. In code the vote thingy would be:

if (pollingPlace.isFine()) {
  vote();
}
//nothing, because you need no dontVote() method or similar, you just don't call vote()

But again, the question is pretty misleading indeed, as mentioned by everyone.

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/cdhoff_ May 16 '19

See reply to doug3465. I agree it is a terribly written question.

3

u/dusty-trash May 16 '19

"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."

This is wrong. It probably said .equals(), not =.

= is used to assign a value, it has nothing to do with comparisons whatsoever.

1

u/cdhoff_ May 16 '19

What "It" are you referring to? The lesson, the original question, or the response? I will have to look back on the lesson and question once I am done with my quiz, but I just pasted the teachers response and there is a screenshot of the original question.

1

u/dusty-trash May 16 '19

The lesson/quiz or wherever the question came from.

1

u/cdhoff_ May 16 '19 edited May 16 '19

https://imgur.com/a/tbV4ELw

After I am done with what I am currently working on, I will look back at the lesson to see if it says anything about .equals(). I think it might have. The question itself had the = symbol, not .equals().

1

u/morhp Professional Developer May 17 '19

Sorry, all those answers are terrible. The third answer is the most right probably. = never compares anything, it assigns. So the first 2 are clearly incorrect. Saying that = creates a concatenated String is really far fetched, I mean you can write String c = "A" + "B"; and there is a = in there, but that's about it. The `+´ is creating the concatenation.

The third one makes the most sense. Technically = also is an expression, but you usually don't use it as such.

At least that explanation is assuming that you're really learning Java. There are other languages, where = might compare the content and == might compare the memory location. But that isn't the case for Java.

1

u/thekab May 17 '19

Those answers are all wrong.

And == compares the value, with primitives and null that's not a memory location.

1

u/cdhoff_ May 16 '19

Fixed the link

1

u/dusty-trash May 16 '19

The last answer is the most right. (It's also wrong since = can be used for ints, charts, etc and not just Strings)

It does not suggest that = can be used to do any sort of comparison as the others do.

If any other answer is the correct one, it's very wrong

1

u/Tha_Gazer May 16 '19

The it they are talking about is the original question. In java a single = is used only as an assignment operator not a value check.

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/ThisApril May 16 '19

All this is making me wonder when someone explained the concept verbally by saying something along the lines of, "double equals compares memory addresses; dot equals compares what's in the string", and the "dot" was heard as "the", if heard at all.

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:

String a = "word";
String b = ... // from external method or declared here, etc
if (a.equals(b)) { /** code here **/ }

== 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.

int a = 5;
int b = 5;
boolean equalValues = (a == b); // true

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

1

u/ThisApril May 16 '19

it falls flat on its face because checking in probably entails you handing over some form of identification

I've had experiences where I checked in by stating my name and address, and they wrote a voter number or something to state that I had checked in.

I agree that it's not a good analogy, but it's quite plausible that the OP lives in a state where having the "password" of name and address is enough to vote.

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
  1. You know who you are

  2. The polling station knows who you are and every registered voter

  3. The polling station verifies you are you based off of the shared information

Similar with passwords

  1. I submit the password

  2. The program has access to all password records

  3. The program verifies my identity based off of this shared information.