r/learnjava • • 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==s3

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

0 Upvotes

12 comments sorted by

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?

2

u/double-happiness Feb 13 '21

Right, that's the one I was wondering about. My first guess was that that would be the only one to evaluate to true, but then I figured it was kind of a trick question and none of them would, in practice.

So it's saying (I suppose) that both s1 and s3 refer to the same object. s1 is a reference to the new String ("test"), and s3 is assigned the value of s1, i.e. the new String ("test"). Something like that? At first I was going to say that s1 is a reference to s3 but maybe that's not quite right, and it's actually being assigned as a reference to the same object. Does that make any sense?

2

u/qelery Feb 13 '21 edited Feb 13 '21
/* A new String object is created. The variable s1 is assigned 
the VALUE of a reference. This particular reference points to 
the address in memory where the String object is stored. It's 
important to remember that s1 does NOT directly hold a string. 
It holds a reference which points to a place in memory. */

String s1 = new String ("test");




/* The same explanation as above just with a new String object */

String s2 = "test";



/* The VALUE of the reference that variable s1 holds is copied 
and given to s3. s1 and s3 both hold a VALUE that is a reference
to the same object. */

String s3 = s1;
/* These two objects are held in different memory addresses. 
Which means that their references point to different places 
in memory. Which means that the VALUE of their reference 
will be different, so false.

s1==s2



/* s1 and s3 are both reference types, and s1 and s3 have 
the same value (you did that when you assigned s1=s3), 
therefore the value of both of their references are 
the same. Hence, true. They refer to the same place in memory. */

s1==s3



/* It's now obvious why this is false */

s2==s3

2

u/double-happiness Feb 13 '21

Super, thanks.

It's now obvious why this is false

Because their references point to different places in memory, if I'm getting this right.

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 Comparable thing, 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.