Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dan exam dought

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in one of the question

Question 6
class MCZ17 {
public static void main (String[] args) {
String a = "\n"; // 1
String b = "\r"; // 2
String c = "\u000a"; // 3 \u000a = new line
String d = "\u000d"; // 4 \u000d = return
}}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4


its answer is c,d

because

The compiler interprets \u000a as a line terminator. The escape sequence \n should be used instead. Similarly, \u000d is interpreted as a line terminator. The escape sequence \r should be used instead.

i didn't get what he means by line terminator ?
is \n escape sequence is not the same thing as line terminator

please explain in details


thanx and regards,
amit
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

\u000d or \u000c are Unicode escapes. These are useful in the Java code when you can not express the characters in ASCII format in Java Source. As the Jav compiler translated it into there actual Unocode before compiling the code, The use of these codes in string or character constants are invalid. ( Becalues these code actualy place newline in the source, thus break the String in two lines).

I hope this will help

Thanks.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls expain in details ...if possible...

please didn't get it...as in some question...
values in string like
String a= "'\u0003'" is right

i don't know why

pls explain... here also unicode is included
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to compile it and look at the errors?

C:\_Work\java>javac MCZ17.java
MCZ17.java:5: unclosed string literal
String c = "\u000a"; // 3 \u000a = new line
^
MCZ17.java:6: unclosed string literal
String d = "\u000d"; // 4 \u000d = return
^
MCZ17.java:8: unclosed string literal
^
MCZ17.java:9: unclosed string literal
^
MCZ17.java:10: ';' expected
^
5 errors


If this represents a new line:

I guess it could be written like this for comparison
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic