I’m working on an old exam for a test and this code is printing the value of 10 for longVariable. Now, by hand, to me, the math would go 9 + 1 % 10 = remainder of 0, not 10… How am I wrong on this?
Thank you for helping!
public class ExamSectionA
{
public static void main(String[] args)
{
int intVariable1 = 9;
int intVariable2 = 10;
double doubleVariable = 11.2;
char charVariable = 'A';
long longVariable;
longVariable = intVariable1 + 1 % intVariable2;
intVariable2 = (int) (doubleVariable / 10f);
String[] theirSalary = {"10","20","30","40"};
System.out.println(intVariable2);
System.out.println(longVariable);
}
}
EDIT: PEMDAS. Think I got it.
Watch your order of operations:
is:
So
9 + 1 % 10is being parsed as9 + (1 % 10). Which gives9 + 1 = 10.%has higher precedence than+.