About Question enthuware.ocajp.i.v7.2.1183 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1183 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1183 :

Post by gparLondon »

This came into my mind so came to look for an answer. Your explanation here on double was very useful, Thank you.

Steffe
Posts: 5
Joined: Tue Dec 08, 2015 9:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1183 :

Post by Steffe »

>> Given the following LOCs:

What's a LOC please?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1183 :

Post by admin »

If you like our products and services, please help us by posting your review here.

Steffe
Posts: 5
Joined: Tue Dec 08, 2015 9:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1183 :

Post by Steffe »

Ah thanks, not come across that abbreviation before.

Post Reply

Who is online

Users browsing this forum: No registered users and 233 guests