1) only float a = 1.01f has on compile error? 2) i think there won't be error coz i have assign b value to a? although it doesn't make sense 3) actually i am confused about the uni code. thanks dirk! =)
The problem with (2) is that the if statement requires a boolean. You are saying if(2) which doesn;t work in Java. This will print true... see if you can tell why? boolean a = false; boolean b = true; if (a=b) System.out.println(a);
I wonder why float a = 1.0, float a = 3.04d, or float a = 0x0333 doesn't work i got what u mean for (2) now. thanks tom!
Joined: Dec 10, 2001
In Java, floating point literals (numbers like 1.0, 2.4, 0.002) are by default considered to be doubles. You cannot implicitly downcast a double to a float. You must explicitly declare that you really want only float data type precision.
I wonder why float a = 1.0, float a = 3.04d, or float a = 0x0333 doesn't work The last one of those does work. Well, if you put it on a separate line ending in a semicolon. 0x0333 is an integer literal, and the compiler's perfectly happy to implicitly cast an int to a float, with no explicit cast from you. For the other two: You must explicitly declare that you really want only float data type precision. Adding to Dirk's point - you can do this by either casting, or using an "F" or "f" to create a float literal. E.g. float a = 1.0F; or float a = (float) 1.0; (Personally I prefer the former, but either works fine.)
"I'm not back." - Bill Harding, Twister
Joined: Jan 30, 2000
Regarding the Unicode example: char a = '\u10100'; If you read about the \u notation, you see it always uses exactly four hexadecimal digits after the \u. So in \u10100, only \u1010 is considered an escape sequence, and it's equivalent to a single letter. This letter turns out to be part of the Myanmar alphabet - let's call it letter "X" since most of us don't have the correct fonts installed to display it anyway. Anyway in \u10100 the final 0 is still left over, not part of the Unicode escape. And the whole line of code is equivalent to char a = 'X0'; The problem now is that you have two characters - X and 0 - inside the single quotes of a character literal. This makes no sense - a character literal can have only one character in it. So you get the message "unclosed character literal" because the sompiler expected 'X to be followed with a closing ' rather than 0.