d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question About Assigning Floats In Java
Add Reply New Topic New Poll
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Oct 28 2015 02:14am
When assigning a float... for instance


Long = L;
L = 564732L;


is it always necessary to put 'L' at the end of the value (564732).....? and what about doubles? is it the same or?









Also can someone tell me what's wrong with my code here:



Code
class LtoD {
public static void main(String args[]) {
Long L;
Double D;

L = 100123285L;
D = L;

System.out.println("L and D: " + L + " " + D);

}
}


This post was edited by ferf on Oct 28 2015 02:21am
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Oct 28 2015 02:45am
figured out the problem in my code.... Long should be long... and Double should be double..... But i still need answers for my question about assigning float types
Member
Posts: 29,367
Joined: Mar 27 2008
Gold: 504.69
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 28 2015 07:03am
The java compiler interprets any decimal number as a double, so you have to add an f at the end to tell the compiler to treat it as a float.

For example...

float f = 1.4; // this will give a type mismatch error because the compiler thinks 1.4 is a double

float f = 1.4f // this tells the compiler that 1.4 is a float
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll