The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

I'm not surprised!!!

Posted by Kishori Sharan on November 10, 2000 at 2:57 PM

When you compile a java program first of all it translates the Unicode escapes ( e.g. \u000a ). At this stage it doesn't differentiate between comments and non-comments lines. So it is immaterial whether you write
char c = '\u000a' ; or
// char c = '\u000a' ;
you will get error.\u000a is a linefeed character and so at first stage of transalation \u000a is transformed into an actual linefeed and at later stage of compilation the linefeed becomes line terminator. So the character literal \u000a is not valid because by the time it is about to be compiled you line is tranlated as
char c = '
' ;
or
// char c = '
' ;

Note the two lines translation of your one line code which is obviouly wrong. That is why in java we have Escape Sequences like \b, \t, \n, \f etc. Since \u0002 is \n , you are supposed to write char c = '\n' ; . You are also not supposed to write char c = '\u002d' ; . You should use '\r' instead.
For more details refer to Chapter 3 "Lexical Structure" ( Section 3.10.4 Character Literals ) of Java Language Specification.
The same argument holds true in case of String as
// String str = "\u000a" ;
Of cource, in case of using multiline comments /* .. */ it doesn't matter.

Thanx
Kishori



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us