Start New Discussion within our Software Development Community

I got quick question regarding Java less say i got provided with this code:

public void printNext(int x){
System.out.println((x+1) % 3);
public void printSequence(){
for (int i=0; i<5; i++) printNext(i);

The question is : 'What sequence of number is printed by printSequence()?'
Ok first its going do 0+1=1 and then invoking printNext method 1/3=0.333... (because of % operand means dividing) but the answer is 1,2,0,1,2 where did one came from 1/3 reminder is 3 not 1?

/ means divide! % means remainder
1/3 = .33333, truncated to 0 in int arithmetic
1%3 = 1 (3 into 1 goes 0 times with a remainder of 1)

This article has been dead for over six months. Start a new discussion instead.