Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
public class TestChar { public static void main(String[] args) { byte A = 1; byte B = \u0041; // 1 System.out.println(B); } } What happens when we compile and run this code 1. prints 65 2. prints 1 3. gives compiler error at line marked 1 4. runtime exception at line 1 cannot assign char to byte variable
this would be true if \u0041 was really '1'. Actually it is the character 'A' and therefore will give a compiler error. The value for '1' is really \u0031!
public class TestChar { public static void main(String[] args) { byte A = 1; byte B = \u0041; System.out.println(B); } }
The above code compiles and prints 1. But I noticed that if the value of �A� (can vary from �128 to 127) changes, printed value of B also changes accordingly ie the printed value of B is exactly same as the value of variable �A�. Actually B has Unicode value that represents character A (\u0041) & byte variable A is referring to value 1 & this is the value printed for variable B. However if byte B is assigned Unicode value of numbers (0 to 9) ie from \u0030 to \u0039, it prints the no.s 0 to 9 correspondingly.
The same thing happens in following cases. byte B =3; byte x=\u0042; // as this represents Unicode value of letter B System.out.pritln(x); //prints 3 byte Z=8; byte x=\005A; // this represents Unicode value of Z System.out.pritln(x); //prints 8 This is true for letters from A to Z with Unicode value ranging from \u0041 to \005A & a to z with Unicode values ranging from \0061 to \007A. Why is this happening here?
The compiler translates Unicode escapes to the corresponding Unicode characters.
byte Z=8; byte x=\005A; // this represents Unicode value of Z System.out.pritln(x); //prints 8
\u005A is translated to the variable Z, and thus Z is assigned to x , and its value is printed. _________________________________________________ By the way, Welcome to the Ranch Jerry and GM. [ April 24, 2003: Message edited by: Jose Botella ]
I'm confused. When using the JDK 1.4, if I try to compile this: int q=\u0041; it complains it can't resolve the symbol. If I use Visualage for Java, it compiles. Any ideas why the JDK does not work? Thanks Julian