Understanding Casting in Java -
this first post here , thank people can me simple question: how casting works in java?
i made simple class:
public class test { public static void main ( string[] args ) { system.out.println((short)(1/3)); system.out.println((int)(1/3)); system.out.println((float)(1/3)); system.out.println((double)(1/3)); } } and piece of software gives me output when executed ( official jdk 6 u26 on 32 bit machine under linux )
0 0 0.0 0.0 the problem, or thing don't understand if would, last 2 results 0.0, expecting 0.3333333, apparently cast works in way: how?
thanks
ps i'm not familiar english language, if made errors apologize this
first, expression 1/3 executed. expression means "integer division of 1 3". , result integer 0. result (0) cast double (or float). , of course leads 0.0.
you must cast 1 of operands of division double (or float) transform double division:
double d = ((double) 1) / 3; or write
1.0 / 3
Comments
Post a Comment