Hi,

The reason why the answer is 1.0 is due to the declaration of your numerator 
and denominator as integers. Remember that 3/2 as integer is 1, the casting is 
done after this result. Thus 1.0 as double;

Its more like:  
= double(3/2);
= double(1);
=1.0;

Let me know if it makes sense or if I am right;

R.R
Sent via my BlackBerry from Vodacom - let your email find you!

-----Original Message-----
From: Venkat Devarapally <venkata.devarapa...@gmail.com>
Sender: javaprogrammingwithpassion@googlegroups.com
Date: Sun, 6 Mar 2011 19:55:17 
To: <javaprogrammingwithpassion@googlegroups.com>
Reply-To: venkata.devarapa...@gmail.com
Subject: [java programming] Casting Question

I am working with Primitive casting covered in LAB:1011 WORKING WITH JAVA
CLASSES...in 5.1

public class CastingPrimitives {

    public static void main(String[] args) {

        // Implicit casting example 1
        int     numInt = 10;
        double  numDouble = numInt;
        System.out.println("int " + numInt + " is implicitly casted to
double " + numDouble);

        // Implicit casting example 2
        int    numInt1 = 3;
        int    numInt2 = 2;
        double  numDouble2 = numInt1/numInt2;
        System.out.println("numInt1/numInt2 " + numInt1/numInt2 + " is
implicitly casted to double " + numDouble2);

        // Explicit casting example 1
        double  valDouble = 10.12;
        int     valInt = (int)valDouble;
        System.out.println("double " + valDouble + " is explicitly casted to
int " + valInt);

        // Explicit casting example 2
        double x = 10.2;
        int y = 2;
        int result = (int)(x/y);
        System.out.println("x/y " + x/y + " is explicitly casted to int " +
result);
    }

}

Answer/Solution:
int 10 is implicitly casted to double 10.0
*numInt1/numInt2 0 is implicitly casted to double 0.0*  (how????) I compiled
the program it still gives me 1.0 and shouldn't it be 1.5 ?? or if 1.0 why??
double 10.12 is explicitly casted to int 10
x/y 5.1 is explicitly casted to int 5

Thanks in advance

-- 
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

-- 
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to