Assigning New Values to Variables in Java

Viewed 4704

I am new to Java, and have a question about variables. Here is an example.

int hello = 6;
int goodbye = 7;
int combined = hello + goodbye;

    System.out.println(combined);
    hello = 10;
    System.out.println(combined);

When I reassign hello, and give it the value of 10 instead of six, and I print combined the second time, it still says combined is equal to 13, not 17 in the console. How do you fix that? Thank You!

5 Answers

You need to recalculate combined because re-assigning a value to hello doesn't mean combined will get updated too. The value for combined is the one calculated using the current value (at that moment) of hello.

int hello = 6;
int goodbye = 7;
int combined = hello + goodbye;

System.out.println(combined);
hello = 10;
combined = hello + goodbye; // recalculate
System.out.println(combined);

combined won't changed just because one of the variables determining it changed. You'll need to manually update combined every time.

For this simple case, the simplest way would just be to write hello + goodbye again as shown in @lealceldeiro's answer.

For more complicated bits of code though, instead of duplicating the same code multiple places, it would be better to wrap that code in a function:

public int doSomeMath(int x, int y) {
    return x + y; //Pretend this is some complicated equation 
}

Then, when you need to do the math later multiple times, you use the function:

int hello = 6;
int goodbye = 7;

// Prints 13
System.out.println(doSomeMath(hello, goodbye));

goodbye = 10;

//Prints 17 
System.out.println(doSomeMath(hello, goodbye));

You are passing int types, which will do everything by value. This means that it assigns combined a copy of the values of hello and goodbye added together.

They are totally separate and unconnected.

Extra Reading

You are printing variable combined not variable hello. To see change in combined you need to recalculate using expression.

int hello = 6; // hello has 6
int goodbye = 7; // goodbye has 7
int combined = hello + goodbye; // combined = 6 (value of hello) + 7 (value of goodbye) = 13

System.out.println(combined); //hence this prints value 13 which is assigned to combined
hello = 10; // changing the value of hello to 10.
System.out.println(combined); // since there is no change in combined value you will get 13 itself.

//In order to change the value of combined you need to assign the value again. In this case you need to re-evaluate combined.
combined = hello + goodbye; //Since now value of hello is 10.Expression will be 10 + 7 = 17
System.out.println(combined);//Output will be 17.

You can think of combined (or variables in a general sense) as a placeholder for integer values. So when executing int combined = hello + goodbye; you're filling the integer placeholder named combined with the sum of the values stored in the placeholders hello and goodbye. When you later execute hello = 10;, you're only putting a 10 in the placeholder named hello. This doesn't affect combined placeholder at all.

So resuming: when writing int combined = hello + goodbye; you're not saying that combined is the operation hello + goodbye but the result of that operation at the time when it was executed.

Related