• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why does java give an error in the comment?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
public static void main (String[] args) {
String a = "\n"; // 1
String b = "\r"; // 2
String c = "\n"; // 3 \u000a = new line
String d = "\u000d"; // 4 \u000d = return
}}


this code was supposed to give an error only on line 4. but it also gives an error on line 3 in the comment on "=" saying illegaL start of expression.

any clues?

Radiya
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unicode escapes are turned into the characters they represent very early in thr translation process. The \u000a in the comment gets turned into a newline, so that the line ends up as

String c = "\n"; // 3
= new line

and this is what the compiler complains about -- that "=" at the start of a line.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
public static void main (String[] args) {
String a = "\n"; // 1
String b = "\r"; // 2
String c = "\n"; // 3 \u000a = new line
String d = "\u000d"; // 4 \u000d = return
}}

Can anyone please explain why line 4 will result in a compile time error?

Thanks in advance

Kayal
 
Radiya Sojitrawala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its the same reason.

the unicode escape sequence is translated earlier..

the \u000d is equivalent to return and it is ranslated to it .. but as there is no escape for return we get an error

HTH

Radiya
 
Radiya Sojitrawala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUOTE] Unicode escapes are turned into the characters they represent very early in thr translation process. The \u000a in the comment gets turned into a newline, so that the line ends up as ...


I used the multi-line comment and it does not give an error then .. so it does not translate to unicode now .. wierd!!
 
Kayalvizhi Umashankar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are refering to the comment portion, yes i understand that.

But i meant why
String d = "\u000d"
this line results in a compile time error

Kayal
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Radiya:

I used the multi-line comment and it does not give an error then .. so it does not translate to unicode now .. wierd!!



No, it still does get translated, but think about it: newlines are allowed inside multi-line comments, right? So adding one more newline doesn't change anything.

char c = '\n'; /* c is \u000a */

becomes

char c = '\n'; /* c is
*/

which is perfectly legal!

By the way, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- a single name is not acceptable. You can fix your display name here. We take this rule quite seriously. Thanks for your cooperation!
 
Radiya Sojitrawala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I mean the very line
String d = "\u000d"

here (as pointed out by Earnest before) unicode is translated first. So the unicode \u000d gets translated to a newline or return character.
Now its equivalent to String d = "newline" and there is no escape sequence(\).. so the error
 
reply
    Bookmark Topic Watch Topic
  • New Topic