• Post Reply Bookmark Topic Watch Topic
  • New Topic

A pretty absurd question  RSS feed

 
Ricky Rozay
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are these two not the same?

s = size
and
size = s ???

The second, size = 2, worked, whereas s = size did not.
Please explain, THANKS!
 
Anniruddh Rana
Greenhorn
Posts: 18
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if by "s" you mean a numeric literal like 2; then I am afraid to say that you can only assign values to variables and not literals.

eg: int size =2; works because size is a variable of type int, so you can assign it a value of integer literal 2.

But since 2 is not a variable you cant say 2 = size;

alternatively.

If you have two integer variables;

int s = 0;
int size =2;

then you may very well go ahead and write s = size; OR size =s;

provided both of them have been declared above.

Kindly elaborate the question if my understanding is wrong.

Anniruddh
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two statements that you mention:

  • s = size;
  • size = s;

  • do two different (in fact, opposite) things. The first sets the variable s to the value of the variable size. The second does the opposite thing: it sets the variable size to the value of the variable s.

    But in your next sentence you are creating confusion, because you're suddenly talking about size = 2 which is different from the two statements you mentioned first.

    Can you please clarify what you mean? How are s and size declared in your code?
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You think the = sign means what it does in maths, ie equals. It doesn’t. It is the assignment operator, which takes the value on its right and uses the value on the left as a name for it.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    imagine you have two documents open in a text editor. Let's call them "document A" and "document B".

    If i said to you "highlight all of document A and paste it over everything in document B", then you would agree that both document A and document B now both contain the same thing - the contents of what was in A.


    If instead i said to you "highlight all of document B and paste it over everything in document A", then you would agree that both document A and document B now both contain the same thing - the contents of what was in B.

    THAT is the exact same thing as the difference between your

    s = size
    and
    size = s

    the first one copies whatever is in size and puts it in s - the contents of s are now lost forever.

    The second copies whatever is in s and puts it in size - the contents of size are now lost forever.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!