answersLogoWhite

0


Best Answer

it is the same as an int, String, or any of the others:

float myVar = 5.7f;

If you try to assign a decimal number you must place an "f" at the end, otherwise Java will assume you are trying to assign a double.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

float is a primitive datatype in java that is used to hold numeric values that contain decimal places.

Ex: float f = 10;

The above is an example declaration of a float variable. They can be used inall arithmetic operations like addition, subtraction, multiplication, division etc.

It uses 32 bits and there is as such no minimum or maximum value it can take. The value that a float can hold is larger than all possible numeric values that we can use in our systems (in most cases)

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Perhaps you got an error message about a possible loss of precision?

The following will generate an error:

float f;

double d = 5.0;

f = d; // Here, there may be a loss of precision. The Java compiler will protest.

To solve this, tell Java that you (the programmer) know what you are doing, by using an explicit cast:

float f;

double d = 5.0;

f = (float) d; // Here, "d" is explicitly converted to type "float"

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to declare a float variable in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you declare an array of 8 floats?

In C float a[8]; In Java float a[] = new float[8];


How do you declare a variable with initial value of 20.1234?

float myVariable = 20.1234;


What is out of scope in returning objects in java?

if u declare variable in method & tray to use this variable outside the method then it is out of scope


How do you declare a pointer variable in c?

int* pint; // instantiate a pointer to an int. float* pflt; // instantiate a pointer to a float.


Which qualifier is used to declare read-only variable in java?

A variable declared as final can't be modified, once a value is assigned.


How do you declare many float variables in java?

You can declare them one by one. However, if you want to store lots of related data, you may want to consider using an array, where you use a single variable name (for example) for 1000 different items, and a number called an index to access the individual items.


Can a double value be assigned to float variable?

THIS IS FOR JAVA i don't know about anything about other languages yes it can be assignedthe syntax is:int (number) = (float) numberFOR EXAMPLE:int = a;a = (float ) 5.5;if the (float) is not there then in Java it gives an error saying precision loss of data type


How do you return an object in java?

With the command return, followed by an object variable. In the method header, you have to declare the return type as the class of the object.


How do youdeclare a variable in jsp?

You declare a variable the same in a JSP as you do in a servlet. Let's say you want to declare a String variable called "foo" and you wanted to assign it a value of "bar." You would do this: String foo = "bar"; Of course, in a JSP, any Java code needs to be enclosed within <% and %>.


Is float is a keyword in java?

yes, float is keyword and data type in java


What is native variable in java?

native is a key word used in java method. there is no variable as native in java


Which keyword is used to declare constants and variable?

Constants are defines using the final keyword.Variables are defined using the one of the keywords:charbooleanintdoublelongintStringTo use a constant you would have to put in something likedouble final pi = 3.14;