r/learnjava • u/double-happiness • Feb 13 '21
String comparison - would any of these evaluate to true?
This is a slide from my course: https://i.imgur.com/LxGBpPP.jpg
String s1 = new String ("test");
String s2 = "test";
String s3 = s1;Then they ask how would these evaluate?
s1==s2
s1==s3
s2==s3I am guessing (but really not sure) that these would all evaluate to false? I suppose because they are all references to different objects, or something like that? I could do with checking, because teach doesn't appear to have given us the answer. TIA.
2
u/JiEToy Feb 13 '21
Just one more thing to add upon the other explanations: the reference being the value of the variable is the reason that in Java you wouldn’t use s1 == s2, but s1.equals(s2).
1
u/double-happiness Feb 13 '21
Yeah, I got that, thanks. IIRC the next thing he goes on to in the slides is the
Comparablething, so it's pretty obvious what he is getting at.
2
u/carrdinal-dnb Feb 13 '21
The main thing this is trying to teach you is the difference between using == and .equals(). == is for primitives and .equals() is for objects.
1
u/double-happiness Feb 13 '21
Yeah, I understand that, thanks. TBH I am a little shaky on the precise meaning of 'primitives' and 'objects', but I figure that so long as I know when to use
==, and when to use.equals()that is good enough for the time being. I find some of the Java terminology a bit abstract and opaque, but I seem to do better just practising writing code and just learning by imitation, if that makes any sense.
2
u/desrtfx Feb 13 '21
How about just trying them out in an IDE? You can use a simple online IDE, like Ideone
All you need to do is paste your first three lines, and then wrap the other three in System.out.println statements.
Then, you have confirmation.
1
u/double-happiness Feb 13 '21
It's not really the correct answers that I'm after, but more a detailed explanation of exactly why they are correct. Anyway, I've got it now, I think, but thanks anyway.
1
u/AutoModerator Feb 13 '21
It seems that you possibly have a screenshot of code in your post String comparison - would any of these evaluate to true? in /r/learnjava.
Screenshots of code instead of actual code text is against the Code posting rules of /r/learnjava as is outlined in the sidebar - Code posting.
No screenshots of code!
If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.
If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.
For small bits of code (less than 50 lines in total, single classes only),
the default code formatter is fine
(one blank line before the code, then 4 spaces before each line of code).Pastebin for programs that consist of a single class only
Gist for multi-class programs, or programs that require additional files
Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.
Ideone for executable code snippets that use only the console
Please do not reply to this message, because I am a bot.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/qelery Feb 13 '21
Your reasoning is correct for the first comparisons the last comparisons.
But take another look at the second comparison. What makes you think they are different objects?