• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Assigning variable data

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 77152
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 77152
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 77152
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic