About Question enthuware.ocajp.i.v7.2.1183 :
Posted: Sun Apr 20, 2014 7:31 am
Further details:
It is just one of the rules of the java language that to get a double out of any expression at least one of the terms has to be a double or float. So 10/100 will not give you 0.1. It will give you 0. If you had 10.0/100, then you would get 0.1. In other words, if all the operands on an operator are int, or byte or short or char (i.e. no operand is a float or a double), an integral operation will be performed and an int will be returned.
Therefore, you should see it like this:
amount = 1 - rate/100*1 - rate/100;
amount = 1 - 10/100*1 - 10/100;
amount = 1 - 0*1 - 0;
amount = 1 - 0 - 0;
amount = 1;
Since the value of the right hand side expression is an int, you can define amount as int, long, or float as well besides double. Had the expression returned 1.0 i.e. a double, you would have had to define amount as double.
You may want to read more here: http://docs.oracle.com/javase/specs/jls ... ls-15.17.2
HTH,
Paul.
It is just one of the rules of the java language that to get a double out of any expression at least one of the terms has to be a double or float. So 10/100 will not give you 0.1. It will give you 0. If you had 10.0/100, then you would get 0.1. In other words, if all the operands on an operator are int, or byte or short or char (i.e. no operand is a float or a double), an integral operation will be performed and an int will be returned.
Therefore, you should see it like this:
amount = 1 - rate/100*1 - rate/100;
amount = 1 - 10/100*1 - 10/100;
amount = 1 - 0*1 - 0;
amount = 1 - 0 - 0;
amount = 1;
Since the value of the right hand side expression is an int, you can define amount as int, long, or float as well besides double. Had the expression returned 1.0 i.e. a double, you would have had to define amount as double.
You may want to read more here: http://docs.oracle.com/javase/specs/jls ... ls-15.17.2
HTH,
Paul.