How do you produce a double in Java

0 votes

I have the following code where the value of d is 0.0. 

For this code block:

int num = 5;
int denom = 7;
double d = num / denom;

I want to force cast this:

double d = ((double) num) / denom;

Can someone tell me is this the correct way?

Oct 3, 2018 in Java by 93.lynn
• 1,600 points
36 views

1 answer to this question.

0 votes

double num = 5;

When you do this, it avoids the casting. For more information, you can check the casting conventions here: https://docs.oracle.com/javase/specs/jls/se7/html/index.html

According to convention 5.2:

Widening primitive conversions do not lose information about the overall magnitude of a numeric value.

[...]

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

Thus you can express 5 exactly as a double.

answered Oct 3, 2018 by anto.trigg4
• 3,440 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 46,730 points
257 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,080 points
72 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
153 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,560 points
84 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,400 points

edited Nov 6, 2018 by Omkar 13,193 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
2,711 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
6,808 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
982 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look here: https://github.com/burningwave/core/wiki/USE- ...READ MORE

answered Dec 20, 2019 in Java by Roberto
• 300 points

edited Dec 31, 2019 by Roberto 2,284 views
0 votes
1 answer

Setting time zone of a java.util.Date

You can make use of the following ...READ MORE

answered Aug 14, 2018 in Java by anto.trigg4
• 3,440 points
5,868 views