I am learning java,

in i know in C,

when we use: float and double ...
the format is :

%f for float

%lf for double.


in java i don't understand why we use

double rather than float??

and the format for double is %f !!!

  1. Can some1 explain why we use double and where float is used in java?
    What are the data types and format specifiers for them ? if you know please post a link where i can read about it?

There isn't a distinct format specifier for doubles as opposed to floats. The %f is used because C programmers are used to it. Really, it's the right thing: it means "floating-point number goes here", and a double is just a sort of floating-point number - one which uses twice as much space to get a lot more precision.

We generally use doubles in Java, honestly, because Java programmers are often unaware of memory tradeoffs, because the language lets them be. You never have to think about the fact that a double takes twice as much space on the stack when you're using it, because you're never really thinking about bytes in Java, and because it very rarely makes a noticable difference. Now, since you're rarely going to actually need the precision a double gives you over a float, it's probably sensible to just use floats by reflex, but nobody does, and the world doesn't end because of it.

i got it.. then whts the benefit of float in java.. where do we usually use float keyword?

Member Avatar for coil

Floats are very rarely used. If space is an absolute priority, using floats does save some space. It's a bit like int/short/byte. We mainly use ints, but shorts and bytes are used to save space.

thanks..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.