| Author |
casting - from Dan's mock casting1
|
Ioana Danescu
Greenhorn
Joined: Aug 28, 2002
Posts: 9
|
|
Question number 6: class Sienna { static double a; static float b; static int c; static char d; public static void main(String[] args) { a = b = c = d = 'a'; System.out.println(a+b+c+d == 4 * 'a'); } } What is the result of attempting to compile and run the above program? a. Prints: true b. Prints: false c. Compiler error. d. Run time error. e. None of the above. the answer is Prints: true, but I get a compile error on a = b = c = d = 'a'; saying 'possible loss of precision, found int require byte', about variable c. [ August 28, 2002: Message edited by: Ioana Danescu ]
|
 |
Ioana Danescu
Greenhorn
Joined: Aug 28, 2002
Posts: 9
|
|
If I change both c and d to be byte: static double a; static float b; static byte c; static byte d; then it works and it prints true.
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
Ioana, Welcome to Javaranch The code compiles fine on my machine. What a = b = c = d = 'a' does is perfectly legal. 'a' is a char literal which gets assigned to d (a char primitive variable). d gets in turn assigned to c which is an integer primitive variable. Assigning a char value to an int variable is legal (widening primitive conversion). c gets in turn assigned to b which is a float primitive variable. Assigning an int value to a float variable is legal (widening primitive conversion) b finally gets assigned to a which is a double primitive variable. Assigning a float value to a double variable is legal, too. (widening primitive conversion). Could you please make sure that the code you posted matches the one you are compiling. Also, what version of JDK are you using (java -version)?
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Isabel Wanderley
Ranch Hand
Joined: Aug 24, 2002
Posts: 42
|
|
Hi Ioana, This happened exactly like that with me, but when I cut-pasted again it worked fine! I don't know what was the problem, but it works fine
|
 |
Ioana Danescu
Greenhorn
Joined: Aug 28, 2002
Posts: 9
|
|
Thank you! I was also surprised by the compile error. I have java version "1.4.0_01".
|
 |
Ioana Danescu
Greenhorn
Joined: Aug 28, 2002
Posts: 9
|
|
Thank you, Isabel, Yes, I made a new java file, I typed again everything and now it works fine.
|
 |
 |
|
|
subject: casting - from Dan's mock casting1
|
|
|