float double error

public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
exByte();
	}
	
	public static void exByte(){
		float f=3.4444f;
		float f1=3.4444;
	}

}

Open in new window

i am trying above program. i am getting error like

Type mismatch: cannot convert from double to float

i wonder why i got this error. Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
incompatible types: possible lossy conversion from double to float
0
CEHJCommented:
Casting will make the error disappear
float f1=(float)3.4444;

Open in new window

0
dpearsonCommented:
Decimal values like "3.44444" are by default treated as type "double" in Java.

If you add the "f" to the end, you're saying it's of type "float" (which has less precision).

So these are both fine:
float f=3.4444f;
double d1=3.4444;

but these are not allowed because you're trying to move a double (more precise value) into a float (less precise):
float f1 = d1 ;
f1 = 3.4444 ;

That's why the cast that CEHJ posted is needed.

The other way around is OK because you can move something with less precision (float) into a value with more precision (double) without losing anything:
double d1 = 3.4444f
d1 = f1 ;

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.