Thursday, September 17, 2015

How To Use Variables In Java Programs Post 5

Hi All...!

Sorry for not publishing a post for weeks, little bit busy with my final year examination.

Okay let's get to work, today I'm going to show you how to use decimal values in Java. Previously
we worked with byte, short, int, long all these data types are using for integer values. In Java we can use "float" and "double" to deal with decimal values. Read more about data types in Java Data Types In Java.

Here I'm going to show how to work with "float" data type.

Let's use "float" in code,




Here you can see assigned value for "float f" is 1 but printed output is 1.0, this happens because float use to represent decimal values. No big deal.

Let's assign 1.0 to "float f"




Here comes the trouble for decimal values, as I told you in my early post all integer values by default take as "int" values if those values are in "int" range. In here all decimal values by default take as double values by compiler. That is why above error occurs.

To fix this error we have to type a "F" or "f" after the assigned value,






Let's write a code to add two float values together,





Another ways to do this addition,





No comments:

Post a Comment