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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en