This question already has answers here:
Why doesn't the post increment operator work on a method that returns an int?
(11 answers)
Closed 5 years ago.
private void calculateForEachQuestion() {
...
if (questions.size() >= answers.size()) {
answers.forEach(answer -> {
if (questions.contains(answer))
this.answer.setPoints((this.answer.getPoints())++);
// Variable expected ^
});
}
The error I encountered is:
unexpected type
required: variable
found: value
What is wrong with the statement?
this.answer.setPoints((this.answer.getPoints())++);
The ++ operator only makes sense when applied to a declared variable.
This operator will add one to an integer, long, etc. and save the result to the same variable in-place.
If there is no declared variable, there is nowhere to save the result, which is why the compiler complains.
One way to allow use of the ++ operator in this situation would be (not the most concise code, but to illustrate the point):
int myVariable = this.answer.getPoints();
myVariable++;
this.answer.setPoints(myVariable);
this.answer.getPoints() will return a value, and you can't use increment ++ or decrement -- on that. You can only use it on variables.
If you want to add 1 to it, you can do it as:
this.answer.setPoints((this.answer.getPoints())+1);
I know that, but why?
You can do this:
int x = 5;
x++; // x is a variable and can have value 6
but not this:
5++; // 5 is a value and can't have value 6
Change this:
this.answer.setPoints((this.answer.getPoints())++);
to:
this.answer.setPoints((this.answer.getPoints())+1);
++ will increment a variable with 1, but since this.answer.getPoints() will return a value and its not a defined variable, it won't be able to store the incremented value.
Think of it like doing:
this.answer.getPoints() = this.answer.getPoints() + 1, where would the incremented value be stored?
The error is just in this bit:
(this.answer.getPoints())++
The first part of this (this.answer.getPoints()) creates an rvalue: effectively, an anonymous variable that lasts almost no time.
The second part ++ increments that anonymous variable after it is passed into setPoints().
Then, the anonymous variable (now incremented) is thrown away.
The compiler/IDE knows that you're doing something that will never be used (incrementing a temporary value), and is flagging it.
Related
This question already has answers here:
Replace a character at a specific index in a string?
(9 answers)
Closed 2 years ago.
I also the error "Left Hand Side of an Assignment must be a Variable" for following line
outPutArray.charAt(i)=inputArray[i]
how to resolve this? I have tried putting braces like but of no use
(outPutArray.charAt(i))=inputArray[i]
You are trying to mutate a String object. This is not possible, String is supposed to be immutable. You have to create a new String with the changed content, use a char array instead, or something else that allows you to actually achieve what you are trying to do in the end (which is: given an input String, return an output String where one character is changed).
With that out of the way, you can't do an assignment on the return value of a function. You don't get a reference back that you can change and then some variable gets changed. You get a value back. The great Is Java "pass-by-reference" or "pass-by-value"? question is maybe a good read.
Very silly, but the format should be:
inputArray[i] = outPutArray.charAt(i);
not
outPutArray.charAt(i) = inputArray[i]
putting the function on the left hand side means you are assigning a constant integer to a integer array element. As the integer is constant, it will fail. This is why we put our variables(inputArray[i] in your case) on the left hand side and our functions on the right hand(outPutArray.charAt(i)). Hope you understand!
.charAt(i) function returns the character at position i. It does not return the reference to the character at that index.
Look at this for more details
This question already has answers here:
Why this giving me 0? [duplicate]
(7 answers)
Closed 8 years ago.
Understanding the difference between ++i and i++, the below example still feels counter-intuitive.
Could someone please explain the order of operations and assignments in the following example?
int i = 0;
i = i++;
System.out.println(i); // 0
Namely on line two, why is i not incremented after the assignment?
The simple way to see it is like this:
Step 1: Your int i = 0; line, which (of course) does this:
i = 0
Then we come to the i = i++; line, where things get interesting. The right-hand side of the = is evaluated, and then assigned to the left-hand side. So let's look at the right-hand side of that, i++, which has two parts:
Step 2:
temporary_holder_for_value = i
The value of i is read and stored away in a temporary location (one of the virtual machine registers, I expect). Then the second part of i++ is done:
Step 3:
i = i + 1
Now we're done with the right-hand side, and we assign the result to the left-hand side:
Step 4:
i = temporary_holder_for_value
The key is that last step. Basically, everything to the right of the = is done first, and the result of it is then assigned to the left. Because you used a post-increment (i++, not ++i), the result of the expression on the right takes i's value before the increment. And then the last thing is to assign that value to the left-hand side.
This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 9 years ago.
i have small doubt.why the below code is printing value i=2.
int i=2;
i=i++;
System.out.println(i);
can someone please explain me what is happening in line no 2.
so there is no meaning here of doing ++ here?
Thanks
i=i++;
Because first the assignment happens, then the increment applies.
Something like:
first i gets 2, then ++ operation happens, but results won't be re-assigned to i, so i value will remain as 2.
i = i++; first evaluates the i++ expression, which increments i and evaluates to the value of i prior to the increment. Since you immediately assign to i this value, it resets the value of i so the increment appears to never have happened. i = ++i; would cause the other behavior.
When you are telling i=i++; you are telling the computer to assign i to i, and after that, increment i's value, but it will not affect i, because i's value is 2.
The correct way to do it should be i=++i; meaning, add 1 to i before assigning it to i, or you could simply use i++;
Thanks to all for helping me in understanding the things which was of great value.
I found somewhere nice post on this.
I got the answer from the suggestion given by stackoverflow forum only but there was some clear explanation missing what I feel.
Miljen Mikic suggested link is not working and saying page not found.
Some Clear explanation given for problem below is
int a=2, b=2;
int c = a++/b++;
System.out.println(c);
disassembles to the following.
0:iconst_2 ; [I]Push the constant 2 on the stack[/I]
1:istore_1 ; [I]Pop the stack into local variable 1 (a)[/I]
2:iconst_2 ; [I]Push the constant 2 on the stack, again[/I]
3:istore_2 ; [I]Pop the stack into local variable 2 (b)[/I]
4:iload_1 ; [I]Push the value of a on the stack[/I]
5:iinc1, 1 ; [I]Add 1 to local variable 1 (a)[/I]
8:iload_2 ; [I]Push the value of b on the stack[/I]
9:iinc2, 1 ; [I]Add 1 to local variable 2 (b)[/I]
12:idiv ; [I]Pop two ints off the stack, divide, push result[/I]
13:istore_3 ; [I]Pop the stack into local variable 3 (c)[/I]
14:return
which help me understand much better.
Please add to this If I am wrong in my point.
Thanks for all your answers.
I am a little confused in declaring variable in android, I know two ways of doing it.
1. int EC, CC, PT, HC = 1;
2. int EC = 1, CC = 1, PT = 1, HC = 1;
I want to know the difference between 1 & 2.
Are both the ways same ?
This question is not really related to Android, it is only dependent on Java.
Anyway,
the first approach creates the variables EC, CC, PT and HC. HC gets the value 1.
In the second approach you create all the variables and give them the value 1.
Just do a search for Java and Variables if you want to find some information about the subject. Here are two links for you:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
http://www.roseindia.net/java/master-java/variables-in-java.shtml
On a side note, you should follow the Java naming conventions and keep your variables lowercase, or capitalize the first letter of any subsequent words in your variable name.
No, it's not the same.
In the first case, only HC will have the value 1 assigned. The other variables will be left unassigned.
If these are instance or static variables, that means they will get the default value 0. If they are local variables, you will need to assign them before you can use them.
only HC will be initialized
all variables will be initialized (with value 1)
Assuming instance variables, frst way, EC,CC,PT are ZERO. Second way all are ONE.
Instance variables are assigned to default values, if nothing assigned.
If you define first one as local variable (inside any method), you will get compilation error.
If you declare int EC,CC,PT,HC=1;
The values for EC,CC and PT are Zero(0) and HC value is 1,
If you decalre int EC=1,CC=1,PT=1,HC=1 means all EC,CC,PT and HC values are one.
In the first case, the first 3 variables are uninitialized, that means when object is created compiler will initialize those uninitialize variables with the default values hich is ZERO in case of integer type and the last variable will be initialized with ONE.
In the second case each variable will be initialized with ONE. You can also do it as follows
First declare all variables
int EC, CC, PT, HC;
Then assign value
EC =CC = PT = HC = 1;
The first example is like :
int EC;int CC;int PT;int HC = 1; // and only hc gets the value of 1
The second example is like :
int EC=1;int CC=1;int PT=1;int HC = 1; // all of the vars get the value of 1
In first case, except EC, all are uninitialized.
In second case, all are initialized.
I've got the following line of code:
suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)+1);
in a block where suffix has already been declared as an empty String (""). The block is looking for duplicate file names and adding a number on to any duplicates so they don't have the same name any more.
The line of code above compiles fine, but if I change it to this,
suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)++);
I get Invalid argument to operation ++/--. Since Integer.parseInt() returns and int, why can't I use the ++ operator?
The ++ operator should update the value of its argument, so the argument should have a fixed position in memory to be updated. For this reason, the argument should be a variable*. In this case, the argument is Integer.parseInt(suffix), has no fixed memory address to be updated at all.
Intuitively, Integer.parseInt(suffix)++ is roughly equivalent to Integer.parseInt(suffix) = Integer.parseInt(suffix) + 1. But Integer.parseInt(suffix) is just an integer value, not associated to a fixed position in memory, so the code above is almost the same thing of, let us say, 32 = 32 + 1. Since you cannot assign a new value to 32 (neither to Integer.parseInt(suffix)) then there is no sense in supporting the ++ operator.
The good news is that this does not cause any problems at all! Instead of Integer.parseInt(suffix)++, write Integer.parseInt(suffix)+1.
* Or, as it is most commonly called, an l-value, or an address value.
++ requires an lvalue (an assignable value).
Integer.parseInt(suffix) is not an lvalue.
Note that i++ is not the same as i+1.
Writing i++ is a shortcut for i=i+1; if you were to 'read it in english' you'd read it as "i becomes current value of i plus one"
which is why 3++ doesn't make sense you can't really say 3 = 3+1 (read as 3 becomes current value of 3 plus one) :-)
The int is an rvalue. Since it isn't bound to a variable you cannot use post-incrementation.
++ expects an assignable value, i.e. a variable. Integer.parseInt returns a value that cannot be assigned. If you need a value plus one, use Integer.parseInt(suffix)+1 instead.