Post incrementing same variable not working [duplicate] - java

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 3 years ago.
I have a simple question that I cannot find the answer to in any java videos or forums. If someone can please explain it to me, why my simple post increment is not working. I start with a value of 2 and never see it increment after 1 or 30 lines.
MY CODE
int x=2;
x=x++;
System.out.println("value of x "+x);
System.out.println("value of x "+x);
System.out.println("value of x "+x);
all the answers are 2. What happened to my increment? x never got 1 added to it post the assigning.
this works if I use 2 different variables and x goes up but I need to see it with one/same variable

Remove the x=x++;:
int x=2;
x++;
System.out.println("value of x "+x);
System.out.println("value of x "+x);
System.out.println("value of x "+x);

Related

java pre increment and post increment and self-assignment in loop [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Java increment and assignment operator [duplicate]
(6 answers)
Closed 4 years ago.
I expected post-increment and pre-increment of a variable and assignment of the result to itself to work a bit differently. But while the latter works as expected, the former runs as infinite while loop. Could someone please point out what i am missing here?
int y = 0;
int z = 4;
while(y<z)
{
System.out.println(y);
y =y++;//this prints 0 infinite times, shouldn't why be assigned values 0,1,2,3 with each pass?
//y =++y;//this works as expected
}
Thank you
As described in this StackOverflow answer, post increment works by storing a copy of y, adding 1 and returning the copy. This means that the return value of y++ is not y+1, but still only y. Since you are overwriting y with y++, you are essentially just saying y = y.

What does the colon ":" and the question mark "?" operators do? [duplicate]

This question already has answers here:
What is a Question Mark "?" and Colon ":" Operator Used for? [duplicate]
(7 answers)
Closed 8 years ago.
What does the following line do? Could someone help me write this line in "normal" code?
int change = (Math.random() - 0.5 < 0 ? -5 : 5);
This is a ternary operator the way it works is :
condition ? (things to do if true) : (things to do if false);
In your code what it does is :
if value of Math.random() - 0.5 < 0
then assign change a values of -5
else
assign change a value of 5.
This line takes a random number (between 0 and 1) and subtracts 0.5. If that value is less than 0 then change is set to -5, otherwise 5.
int change;
if((Math.random() - 0.5) < 0)
{
change=-5;
}
else
{
change=5;
}

postincrement operator interesting behaviour in Math.min()

I have a question,
In Java, does Math.min bind tighter than ++?
Let me illustrate with an example and maybe someone can explain to me why I get the results I get.
Here's a method I run:
private static void testIncrement() {
int x=10;
System.out.println(x++);
System.out.println(x);
x=10;
System.out.println("-----------");
System.out.println(++x);
System.out.println(x);
x=10;
System.out.println("-----------\n"+x); //10
x=Math.min(255, x++);
System.out.println(x); **//x=10 WHY NOT x=11?**
x=10;
System.out.println("-----------\n"+x);
x=Math.min(255, ++x);
System.out.println(x);
}
The results are:
10
11
-----------
11
11
-----------
10
10
-----------
10
11
On the line where I put //x=10 WHY NOT x=11?
I wonder why x is 10 and not 11. Maybe someone can explain this to me.
It looks as if Math.min create a copy of x (which is 10 at this time) which it uses to do Math.min. Then the original x is incremented from 10 to 11, but the copy which is still 10 comes out of Math.min and overwrites the incremented one.
Does this make sense?
Does anyone have an explanation for why x is 10 and not 11 in this case?
Thanks
PS - I totally understand How do the post increment (i++) and pre increment (++i) operators work in Java?
Let's deconstruct this line:
x = Math.min(255, x++);
The x++ means "remember the original value of x; then increment x; then the value of the expression is the original value". All of this happens before the assignment. So it's equivalent to:
int tmp = x; // x = 10, tmp = 10
x = x + 1; // x = 11, tmp = 10
x = Math.min(255, tmp); // x = 10
Hopefully that should make it clear. In particular, this has nothing to do with Math.min itself - it's just behaving as a normal method invocation. See section 15.14.2 of the JLS for more details.

What is ++int in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
explain working of post and pre increment operator in Java
What is the difference between int++ and ++int?
In Java, what is ++int? What does it do? What does it mean?
(Sorry, but I didn't ask the question correctly last time.)
a = 5; b = ++a; // a = 6, b = 6
a = 5; b = a++; // a = 6, b = 5
int a=1;
System.out.println(a++);
prints "1"
int a=1;
System.out.println(++a);
prints "2"
Or maybe i don't understand your question.
++int increments int by 1 before and int++ increments by one after

Some java Fundamental [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Update::
1). problem ::
i wrote System.out.println("welcom"); &
System.out.println("india");
i want like :: welcome india
but give::
welcome
india
1.
println always prints a newline character. To get what you want, try:
System.out.print("welcome ");
System.out.print("india\n");
2.
x++ is post-increment, ++x is pre-increment.
So in:
int x = 5;
int y = ++x;
y will be 6.
But in:
int x = 5;
int y = x++;
y will be 5.
In 1: What is the difference between System.out.print and System.out.println ?
In 2: What does the ++ operator do and when does it do it?
println
stands for "print line" so, if you use println you have a "carriage return" and the next println will print a new line (and so on...)
x++ is different to ++x: the second increment variable's value and then goes on, the first goes on with statement and then increment variable's value.
System.out.println("welcom"); & System.out.println("india");
you wrote now if you want same line
System.out.print("welcom ");
System.out.print("india");
And
x++ is Post-increment
Post-increment : add 1 to the value.
The value is returned before the increment is made, e.g.
x = 1;
y = x++;
Then y will hold 1 and x will hold 2
and
++x is pre increment
Pre-increment : add 1 to the value.
The value is returned after the increment is made, e.g.
x = 1;
y = ++x;
Then y will hold 2 and x will hold 2.
1) To print a string without printing an endline use System.out.print()
2)
x++ - increment x, and return x's previous value (before increment)
++x - increment x, return x's current value (after increment)
These are the Post and Pre fix versions of incrementing.
println() prints a line and anything printed after the println statement (using any print statement) is printed in the next line.
Second thing is the difference between ++x and x++. ++x increases the value of x BEFORE doing anything else in the statement (pre-increment), and x++ increases the value of x AFTER it performs anything else in the statement (post increment).
For example:
x = 1;
y = ++x; // value of y is 2, value of x is 2
but
x = 1;
y = x++; // value of y is 1 and x is 2
try System.out.print & 1 happens before a condition/expression takes place; the other after;
System.out.println() has give next line as it complete statment & ++x are use for preincremet & C++ is post incremental.

Categories

Resources