javascript
Brief description  about Online courses   join in Online courses
View Luvita  Burgess 's Profile

long by default taken as int and float as double

Hello Sir,
I could not assign a value more than 2,147,483,647 to a variable of type long.

long longvar = 2,147,483,648;

I get the compile error as:
MyDataType.java:17: error: integer number too large: 2147483648
long longvar = 2147483648;/*-9,223,372,036,854,775,808 to 9,223,372,03
6,854,775,807*/

Similarly when assign 4.5 to variable of type float
float floatvar = 4.5;

I get the following compile error:-
MyDataType.java:18: error: possible loss of precision
float floatvar = 4.5;
^
required: float
found: double
1 error

I tried even type casting. It still didn't work. Could you please help.
Asked by Luvita Burgess | Apr 7, 2013 |  Reply now
Replies (2)
View ajit mahadeo angaj 's Profile
Hi Luvita,
I checked this. By default numbers with precision point are considered double. There is no implicit way to convert double type value to float type value. But there is explicit way to convert this.
Here is the solution
float floatvar =(float)4.5;
Jun 9, 2013
View java teacher 's Profile
The long data type is a 64-bit signed two's complement integer.
It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

Use this data type when you need a range of values wider than those provided by int.
May 21, 2013