Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- Why the method argument treats the float value as double value? [duplicate]

Tags:

java

casting

I've defined a method with one parameter of type float shown below:

public float getValue(float value){

    return value;
}

When I call this method by passing float value say 10.1201 shown below:

float value =  methodReturnTest.getValue(10.1201);

My IDE says to cast the argument to float. I tried searching but couldn't get the appropriate answer. so posting it. Please explain .

Thanks.

like image 438
Balwant Kumar Singh Avatar asked Jan 31 '14 04:01

Balwant Kumar Singh


Video Answer


2 Answers

Java takes 10.1201 as double. In order to pass a float value you should append f to it like this:

float value =  methodReturnTest.getValue(10.1201f);
like image 88
Vishal Santharam Avatar answered Oct 17 '22 17:10

Vishal Santharam


In java Float Literals are specify by using f at the end otherwise it be treated as double literal.

Do like this

float value =  methodReturnTest.getValue(10.1201f);
like image 4
Nambi Avatar answered Oct 17 '22 18:10

Nambi