• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Nice Question

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it prints 1
since unicode is converted in very early stage of compilation
the line
byte B = \u0041;
becomes
byte B = 1;
and hence the answer
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the real question, "will unicode implicitly convert to a byte?"
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Julian Reed:
If I use Visualage for Java, it compiles. Any ideas why the JDK does not work?


This compiles cleanly in JDK 1.4:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic