This question already has answers here:
Java increment and assignment operator [duplicate]
(6 answers)
Closed 7 years ago.
public static void main(String[] args) {
int x = 10;
x = x++;
x = x++;
x = x++;
System.out.println(x);
}
Why is the output 10 when the expected output is 13?
Post increment operator x++ returns the original value of x. Therefore x=x++ assigns the old value of x back to x.
This is probably what you weant to do
public static void main(String[] args) {
int x = 10;
x++;
x++;
x++;
System.out.println(x);
}
Related
This question already has answers here:
What does a variable being "effectively final" mean? [duplicate]
(2 answers)
Lambdas: local variables need final, instance variables don't
(10 answers)
Closed 11 days ago.
I understand the concept of final / effectively final within lambda , it essentially protects from side effects. However why the following code snippet works? Is it also not violating this behaviour?
import java.util.stream.Stream;
public class Learn {
int x = 0;
Stream<Integer> numbers() {
x = x +1;
return Stream.iterate(0, i -> {
int result = x + i;
x = i;
return result;
});
}
public static void main(String[] args) {
Learn l = new Learn();
l.numbers()
.skip(20) // Don't use the first 20
.limit(10) // Then take 10 of them
.forEach(System.out::println);
System.out.println("=====================");
System.out.println(l.x);
}
}
75025
121393
196418
317811
514229
=====================
317811
This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 4 years ago.
I have a question about Java being pass-by-value. I know that variables declared outside a method will not change their values since when I call I method on the variables, the method will only be using the value assigned to them. But in this case, I do not understand why int result does not get a value of 2. As increment() will get the value of x, so 1 and increment it by 1 and store the value in the result variable.
public class Increment {
public static void main(String[] args) {
int x = 1;
System.out.println("Before the call, x is " + x);
int result = increment(x);
System.out.println("After the call, x is " + result);
}
public static int increment(int n) {
return n++;
}
}
Post increment operator n++ increments the value of n by 1, but returns the previous value. Therefore increment(x) returns x, not x+1.
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 4 years ago.
I'm new to Java programming and I was playing round with a code:
public class Main {
public static void main(String[] args){
byte x = 10;
x = x*10;
System.out.println(x);
}
}
This gives a compilation error and I know why. But what I don`t understand is why the code below does not give an error:
public class Main {
public static void main(String[] args){
byte x = 10;
x *= 10;
System.out.println(x);
}
}
From what I know
x *= x;
and
x = x*x;
are same.
What am I missing then?
The compound assignment operators will perform automatic type-casting. You could find more details on https://www.geeksforgeeks.org/compound-assignment-operators-java/
This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 8 years ago.
I was executing the following code
class First
{
public static void main(String arg[])
{
char x= 'A';
x = x++;
System.out.println(x);
}
}
Here the output is A.
My question is why didn't x get incremented before printing.
You're using the post-increment operator incorrectly - you don't need to use assignment as well. And in this case it undermines what you're trying to do.
For context, remember that the post-increment operator increases the value, and returns the old value. That is, x++ is roughly equivalent to:
int x_initial = x;
x = x + 1;
return x_initial;
Hopefully now you can see why your code is failing to change x. If you expand it, it looks like:
char x= 'A';
char y;
{
y = x;
x = x + 1;
}
x = y;
System.out.println(x);
and the net effect of the assignment, is to set x back to what it was originally.
To fix - you can simply call x++. Or, if you want to make it clear there's some sort of assignment happening, x += 1 or even just x = x + 1 would do the same thing.
class First
{
public static void main(String arg[])
{
char x= 'A';
x = x++; // it is post increment as ++ sign is after x
System.out.println(x);
}
}
Post Increment(x++) : First execute the statement then increase the value by one.
Pre Increment (++x) : First increase the value by one then execute the statement.
This question already has answers here:
Is there a difference between x++ and ++x in java?
(18 answers)
Closed 10 years ago.
What is the difference between x++ and ++x in Java
Can anybody please tell me the difference of the above by refering the below code,
class Example{
public static void main(String args[]){
int x=10;
int y;
y=x++; //Prints 11 10
System.out.println(x+"\t"+y)
}
}
class Example{
public static void main(String args[]){
int x=10;
int y;
y=++x; //Prints 11 11
System.out.println(x+"\t"+y)
}
}
y=x++ assigns x to y, then increments x.
y=++x increments x, and then assigns it to y.
++x is the pre-increment. i.e., The value of x is first incremented and then assigned to x.
x++ is post increment. i.e., the value of x is assigned first and then incremented.
y=x++;
is essentially same as
y =x;
x= x+1;
y=++x; is same as
y= (x+1);
Pre and Post increment. Increment BEFORE assignment and increment AFTER assignment respectively.
The difference is that in the first case (x++) Java first resolves the assignment issue and then increments x. In the other case (++x) Java first resolves the increment and then the assignment.
In the following code you will see the difference:
#Test
public void test1() {
int x = 1;
int y = 1;
y = 2 + x++;
assertEquals(2, x);
assertEquals(3, y);
}
#Test
public void test2() {
int x = 1;
int y = 1;
y = 2 + ++x;
assertEquals(2, x);
assertEquals(4, y);
}
As you can see, x always will be incremented, but the difference is the order in which the expression is resolved.
Hope it would be useful!