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.
Related
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);
}
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:
post increment operator java
(3 answers)
Closed 9 years ago.
public static void main(String[] args) {
int a = 1;
int b = a++;
System.out.println(b);
}
Why does the code above print 1? I was told that a++ means you would get the value of a. then increment it. But the code above never incremented a and just printed 1. Could someone please explain what is going on here?
It works as expected; Here is the code explained:
public static void main(String[] args) {
// a is assigned the value 1
int a = 1;
// b is assigned the value of a (1) and THEN a is incremented.
// b is now 1 and a is 2.
int b = a++;
System.out.println(b);
// if you had printed a here it would have been 2
}
This is the same code but with pre-increment of a:
public static void main(String[] args) {
// a is assigned the value 1
int a = 1;
// a is incremented (now 2) and THEN b is assigned the value of a (2)
// b is now 2 and so is a.
int b = ++a;
System.out.println(b);
// if you had printed a here it would have been 2 so is b.
}
int b = a++ is equivalent to int b = a; a += 1. When you print b, a==2 && b==1
a++ is like saying a-then increment a by 1. So after you say b = a++, b = 1 and a = 2.
If you were to preincrement b = ++a, you would get b = 2 Preincrement increases before it assigns value.
Conclustion
a++ assign value before incrementing
++a increments, then assigns value.
The reason is is as follow.
If it is i++, then equalent code is like this.
result = i;
i = i+1;
return result
If it is ++i. then the equlent code is like this.
i=i+1;
result =i;
return i
So considering your case, even though the value of i is incremented, the returned value is old value. (Post increment)
Post Increment Meaning = First complete assignment then Increment.
Pre Increment Meaning = First Increment then assign .
ex:
int a=0,b;
b=a++; // ie. Step1(First complete assignment): b=0, then a++ ie a=a+1; so a=1 now.
System.out.println(b); // prints 0
System.out.println(a); // prints 1
if
int a=0,b;
b=++a; // ie. Step1(First Increment ): a++ ie a=a+1; so a=1 now, b=a , ie b=1.
System.out.println(b); // prints 1
System.out.println(a); // prints 1
public class recursion {
public static void main(String[] args)
{
thisclass(0);
}
public static void thisclass(int z)
{
int x = 1;
int y = 3;
if (z==10)
{
System.out.println("Done");
}
else
{
System.out.println(x/y);
x++;
y= y+2;
thisclass(z++);
}
}
}
I'm learning recursion right now and when I get to the else statement in the thisclass method, I get an error after it prints an abnormal amount of zeros.
What I want the program to do is run 10 times and do something along the lines of Print 1/3 2/5 3/7 etc.
Where do I go wrong?
This line:
thisclass(z++);
Doesn't do what you think it does. It increments 'z' and then calls thisclass on the original value of z. It's a lot like doing:
int temp = z;
z = z + 1;
thisclass(temp);
You want to use preincrement instead of postincrement here:
thisclass(++z);
The answers that drunkenRabbit and Serdalis have posted are also valid. It won't work correctly until you've made all of these changes.
There are as far as I can tell (other than naming conventions) three things that are causing your program to not run as intended.
(1) You are doing integer division in your print, so 1/3 will be 0 (no decimals in ints).
Solution: Change int x and int y to double x and double y.
(2) You are post incrementing z when you pass it into the recursive call, meaning the recursive call does not see the new value, but rather the old one.
Solution: Pre-increment z thisclass(++z).
(3) You also likely mean to have x and y declared outside of your method, so that their updated values persist. (Instead you would just be printing the same value 10 times).
Solution:
double x = 1.0;
double y = 3.0;
public static void thisclass(int z){ ...
You are getting all those 0's because you are doing integer division, which does not support 1/3 etc.
You should change your code to use float's which will get rid of the 0 problem.
E.g.
float x = 1.0;
float y = 3.0;
You are also resetting the value of y with each call, so y will always be 3 at the beginning of the call and 5 at the end. You should check the value of z to see what the value of y should be.
Same can be said about the value of x.
The z is being post-incremented at each call, which will cause the value of z to not increase with each call, you should change the call to:
thisclass(++z);
To make tha value pre-incremented. Otherwise this call will go on forever.
Also, please don't call your methods thisclass is it very confusing.
Incrementing x and y in the else condition does not affect the recursive call variable. Think of recursion as a new call to the same method.
So, when making the recursive call x and y are initialized back to 1 and 3. You can pass x and y as a parameter so the updated value can be passed to each recursive call. Is one way to go about this...
Hope this helps :)
Notes:
it's better to use an initial call function that calls the recursive function itself, for initialization
use static data, otherwise they won't be preserved through the recursive calls
recur(z++) will execute like recur(z) resulting in an infinite recurrence, use recur(z+1) instead
Code:
public class recursion
{
public static void main(String[] args)
{
startRecursive(0);
}
// using static data
private static int x = 1, y = 3;
// initial call
public static void startRecursive (int initZ)
{
x = 1;
y = 3;
// avoid infinite recurrence
if (initZ > 10) initZ = 0;
recur(initZ);
}
// recursive function
public static void recur(int z)
{
if (z == 10)
{
System.out.println("Done");
}
else
{
System.out.println(x/y);
x += 1;
y += 2;
recur(z+1); // z++ will return z
}
}
}
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!