double vs float

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
hin1129
Posts: 9
Joined: Fri Apr 14, 2023 2:20 pm
Contact:

double vs float

Post by hin1129 »

int rate = 10; int t = 5;
XXX amount = 1000.0;
for(int i=0; i<t; t++) {
amount = amount*(1 - rate/100);
}
What can XXX be?

i am struggling to understand the difference between double and float
i know that they got different sizes:
float is Up to 7 digits after decimal and default value is 0.0f
double is Up to 15 digits after decimal and default value is 0.0d
but how do you tell whether the value is double or float? (apart from the letters d and f)
and for this question why it can only be double?

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

Re: double vs float

Post by admin »

> how do you tell whether the value is double or float? (apart from the letters d and f)
1. When you write any number in your code with a decimal point, it is a double except when you suffix it with f or F. There is no other way.
2. When any number is computed by doing arithmetic operations on any other number which is a double or a float, then the result is a double. e.g. result of multiplying two floats is also a double (not a float).

So, you can see why XXX can only be a double.

>i know that they got different sizes:
Yes, but the size is that of the size of the datatype i.e. 16 bits or 32 bits of 64 bits. It is not the size of the decimal part. The size of float (32 bits) or double (64 bits) may or may not necessarily correspond to the number of digits after decimal.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests