• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning variable data  RSS feed

 
Jordan Tee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The source I'm using to learn java has done a good job of being clear and straight forward, however there is something I do not understand and can't seme to find the answer to. Why would i want to make variable equal to itself? Here is an example of what is confusing me.

int weight = weight + 150;

Why would I not just do:

int weight = 150;

Thanks for the help.
 
Sid Moitra
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan,

Could you please post the entire or some part of the code as this is not making any sense??
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan Tee wrote: Why would i want to make variable equal to itself? Here is an example of what is confusing me.

int weight = weight + 150;


As already mentioned, this is not valid code. For static and instance variables, this is trying to reference a field before it is defined. And for local variables, this is trying to use a variable before it is initialized.

Henry
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you

In a line like weight = weight + 150; you need to remember that the + operator has a higher precedence than the assignment operator =
Work out what weight is at the end of this code
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As others have mentioned, you may have copied that code incorrectly.
 
Jordan Tee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the answer would be 234. This has helped me and I will explain what I understand and please correct me if I am mistaken. You have assigned weight with

int weight = 111;

You later have reassigned weight by modifying it by a value of 123

weight = weight + 123;

I'm assuming this is done so that you can have a starting value and then later modify it more easily. My question now is: after
weight = weight + 123;
runs does the permanent value of weight become 234 or does it remain 111?

Thanks for your help!

Campbell Ritchie wrote:Welcome to the Ranch both of you

In a line like weight = weight + 150; you need to remember that the + operator has a higher precedence than the assignment operator =
Work out what weight is at the end of this code
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan Tee wrote:I'm assuming this is done so that you can have a starting value and then later modify it more easily. My question now is: after
weight = weight + 123;
runs does the permanent value of weight become 234 or does it remain 111?

The = operator (assignment) stores the value of whatever is right of the = in the variable that's left of the =. So this would store the value 234 in the variable named 'weight'.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping you would get 234. That remains the value of weight until you add 111 again, when it becomes 345. Or some other explicit change.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!