• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Why would this not compile?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
class Test {
public static void main(String a[]) {
char c = 'A' ;
// char d = '\u000D';
}
}
</pre>

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

See JLS 3.2, 3.4 for details.
Here is my understanding.
Unicode escape is first translated into correponding unicode character.
So, \u000D is translated into a Carriage Return, which is defined as a Line Terminator.
Therefore, this line looks like two lines to the rest of tranlation process:
// char d = '
'
It's this second line gives you the error - unclosed character literal.
[This message has been edited by Nain Hwu (edited November 09, 2001).]
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's sort of interesting..
I know \u000D is a carriage return and if you try \000A which is a line feed the code also doesn't compile. It's odd because you could do d = '\r' and it'll work fine. I'm guessing when you type chard d = '\u000D' it's as if you typed the ' and then pressed RETURN or ENTER on your keyboard and then typed ' again, since typing \u000D should represent doing a carriage return.
I could be wrong about all this and am interested to learn more about what's really going on.
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick,
You can read JLS chapter 3 for all the details.
Note that unicode \u000a and \u000d are line terminators.
So, for sure you want to avoid using it. Instead, use
the escape sequence \n, \r.
As you can see in this example, using these two unicode charaters in code may generated unwanted result, even in the comment!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic