What does the ++counter mean? [duplicate] - java

This question already has answers here:
++someVariable vs. someVariable++ in JavaScript
(7 answers)
Closed 7 years ago.
I don't consider myself to be bad at programming, but there's been something troubling me since the past few days.
int counter = 3;
++counter;
Is the following code above the same as counter++;.

It is similar, but not the same.
In your expression it doesn't matter, but if you had something more complicated, like System.out.println(counter++), it would make a big difference.
For example:
int counter = 3;
System.out.println(counter++)
This will print 3, then increment counter to 4.
However, if you do
int counter = 3;
System.out.println(++counter)
it will print 4 because it increments prior to giving the value as a parameter to the print function.
It's a question of when the increment is performed, the prefix performs it before other operations, postfix performs it after. They have different precedences.

Related

++ Syntax for Content Inside Array? [duplicate]

This question already has answers here:
What's the difference between i++ vs i=i+1 in an if statement? [duplicate]
(2 answers)
Closed 1 year ago.
can you do array[i]++?
Is it the same as array[i] = array[i] + 1?
If not, why not? Is it something to do with how primitives vs references are treated?
There's a slight difference: postfix vs prefix increment operator
If you use array[i]++, the old value will be used for the calculation and the value of i will be increased by 1 afterwards.
For array[i] = array[i] + 1, it's the opposite: i will first be incremented and only then the calculation will take place.
For more details, check this out.

Can someone please explain how this recursion function works? [duplicate]

This question already has answers here:
How does Recursion actually work in Java?
(4 answers)
Closed 2 years ago.
I just started recursion and happened across a function similar to this, so I adapted it to the question on my homework:
public int getRSquare(int n){
if(n==0){
return 1;
}
return getRSquare(n-1)*n;
}
So when n=5, it returns 120, which is correct, this method outputs the factorial of n. But I don't understand WHY. Doesn't n eventually reach 0, so shouldn't it return 1, not 120? This is java btw, just in case that's needed
on the return value n is updated each time when the method is called. 5-1=4-1=3-1=2-1=2-1=0.
When n is 0 the method stop.

Variable is setting itself to value of another variable [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Assigning in Java?
(5 answers)
Closed 3 years ago.
PS: This question is different than others because I need to know exactly what code to write in order to prevent the variable from changing.
I have this example of code to demonstrate my problem:
float[][] one = {{0}};
float[][] two = one;
System.out.println(two[0][0]);
one[0][0] ++;
System.out.println(two[0][0]);
int three = 0;
int four = three;
System.out.println(four);
three ++;
System.out.println(four);
The output is:
0.0
1.0
0
0
For some reason, when I change float[][] 'one', 'two' automatically changes as well.
However, when I performed the same task but for a different data type int, then the code works as intended.
What is causing this variable to drag the other one with it?
Why doesn't it happen for the int type?
How do I fix it?

Why for goes to infinite loop? [duplicate]

This question already has answers here:
Byte incrementing operation in Java [duplicate]
(4 answers)
Closed 7 years ago.
When I start my code
for (byte i = 0; i < 1000; i++) {
System.out.print(i);
}
I get infinite loop. Why?
Simply because byte value starts to overflow after its max value i.e. 127.
The value of i will go up to 127 and then will overflow to -128 and then increment back to 127. This process will therefore never satisfy your for loop termination condition, and thus loop forever.
The maximum value of a byte is less than 1000.

What's the difference between --i and i--? [duplicate]

This question already has answers here:
Post-increment and pre-increment within a 'for' loop produce same output [duplicate]
(12 answers)
Semantics of pre- and postfix "++" operator in Java [duplicate]
(7 answers)
Closed 9 years ago.
In some code i've seen a for loop with a --i as third parameters
for(int i=array.length; i<0; --i)
Maybe someone can explain me the difference with i-- ?
i guess it's something like the moment when i is decremented ?
If, for example, i = 5:
--i decrements i by 1 then gives you the value of i (4).
i-- gives you the value of i (5) then decrements it by 1.
Both will give you the same result in a for loop.

Categories

Resources