++a is a pre-incrementation. Which means that a is incremented before returning the value of a.
a++ is a post-incrementation. Which means that a is incremented after returning the value of a.
In other words, a++ gives the current value of a and then increment it. While ++a directly increment a. If a=42 then System.out.println(a++) gives 42 while System.out.println(++a) gives 43 and in both cases, a=43 now.
OP also asked for a line by line explanation of that code:
import java.util.Scanner; public class Number { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int number = keyboard.nextInt(); int division1 = (number++) % 10; number = number / 10; System.out.println(number % 10+division1); } }
I guess, only the code inside the main function need some explanations :
// Create a Scanner object that read from the standard input. Scanner keyboard = new Scanner(System.in); // Read an integer. int number = keyboard.nextInt(); // put (number % 10) into variable division1 and then increment `number`. int division1 = (number++) % 10; // Divide number by 10. number = number / 10; // Print that expression : System.out.println(number % 10+division1);
The line int division1 = (number++) % 10; might not be very clear. It would be simpler to read like that:
int division1 = number % 10; number += 1;
Now, the explanation of what the function does:
If number = 142, we put 2 into variable division1, then number is incremented and divided by 10. So number gets the value 14 ((142+1) / 10). And now we print number % 10 + division1 which is 4 + 2 = 6.
Here some examples of results (I've compiled the code myself):
3 => 3 9 => 10 10 => 1 248 => 12
import?