{Tips For Java} Floating-point numbers {beginners} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 38

{Tips For Java} Floating-point numbers {beginners}

Floating-point numbers, are numbers which can represent fractions as well as integers. In Java, the default floating-point type is called double, which is short for double-precision. Here are few points that you need to know about it: The integer value 1 is different from the floating-point value 1.0, even though they seem to be the same number. They belong to different data types, and strictly speaking, you are not allowed to make assignments between types. The following is illegal because the variable on the left is an int and the value on the right is a double: int x = 1.1; // compiler error You may forget this rule because in many cases Java automatically converts from one type to another: double y = 1; // legal, but bad style Java allows it by converting the int value 1 to the double value 1.0 automatically. This is convenient, but may causes problems for beginners. For example: double y = 1 / 3; // common mistake You might expect the variable y to get the value 0.333333, which is a legal floating-point value. But instead it gets the value 0.0. The expression on the right divides two integers, so Java does integer division, which yields the int value 0. Converted to double, the value assigned to y is 0.0. double y = 1.0 / 3.0; // correct Not necessary but always assign floating-point values to floating-point variables.

3rd Mar 2017, 1:40 AM
Ram chandra Giri
Ram chandra Giri - avatar
2 Answers
+ 4
Thanks this post has cleared my doubts.
3rd Mar 2017, 2:34 AM
gaurav kulkarni
gaurav kulkarni - avatar
+ 1
thanks... these floating numbers have any use c programming or not??
2nd Jan 2018, 4:58 PM
subhadip khilari
subhadip khilari - avatar