• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why char c='\u000a'; is not compiling?

 
prem sagar bojja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
Can any body explain me what's wrong with the following program..
public class CharTest {
public static void main(String[] args) {
char c='\u000a';
}
}

gives the error message: invalid character constant.
if char c='\u000d' aslo shows the same error message..
but when the statements
char c='\u000b';
char c='\u000c';
char c='\u000e';
char c='\u000f';
are compiling well..please some body help me in explaining this. Thanks inadvance..
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That character happens to be the newline character, which is not allowed as per the Java Language Specification.
 
prem sagar bojja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer,
Thanks for the reply.
But can you please explain it indeatail?.. when assigned value '\u000a' to 'char c' becomes newline characher, it should not allow the code char c='\n'; to be compiled...Please explain..
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unicode escape sequences like \u000a are replaced by the actual characters they represent before the Java compiler does anything else with the source code. So the \u000a in your source code is replaced internally by a linefeed character. Note that this happens before the compiler actually reads and interprets your source code. After doing this, your program looks like this.

That's not a valid Java program, so the compiler complains.

You can't use Unicode escape sequences in this way to insert special characters like linefeed in your application. Instead of \u000a, use \n for linefeed (or \r for carriage return).
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats because of the Unicode escapes happening before the compilation.

Jesper's explanatino would really help you. To know more about this, please have a look at here.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers,

you may find it interesting that these "forbidden chars" don't even compile in a comment:


this class does not compile.


Yours,
Bu.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Burkhard Hassel:
Howdy ranchers,

you may find it interesting that these "forbidden chars" don't even compile in a comment:


this class does not compile.


Yours,
Bu.


Bu,

Since it appears within the comment, the presence of such character really does NOT make sense right?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:
... Since it appears within the comment, the presence of such character really does NOT make sense right?

It might make sense. For example...

//Use \u000A for new line

But it won't compile.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

It might make sense. For example...

//Use \u000A for new line

But it won't compile.



oh. that's correct. Silly mistake i did!

Thank you Marc.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Marc and Bu,

I found a strange behavior when i am preparing the presentation slides for the fresh buddies.

The same comment containing the line feed character in Unicode form gets compiled when it is present inside the multi-line comment.

Here is the same code but within the multiline comments.



The code gets compiled successfully.

But its guaranteed to fail in compilation when its present in the single line comment. I could not find much information about this in JLS as well.(Chapter 3, Lexical Structure).

Any idea about this?

Thanks in advance.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,

It's not as strange as it looks. \u000a is a newline character. Multi-line comments start with /* and end with */. Newline characters are allowed inside a multi-line comment. These two pieces of code are exactly the same:

They are both valid multi-line comments. A single-line comment starts with // and ends with newline. If you use \u000a inside such a comment, the text after the \u000a is put on the next line and is not interpreted as a comment.

You see, the words "single-line comment" are on the next line, not inside the comment anymore, and since it isn't a valid Java statement, you'll get a compiler error.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's perfect.

Thank you very much Jesper.
 
Peter Mularien
Author
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, this could lead to some interesting trick questions, for example:

... this will of course print 6!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thats true as because of its being a single line comment and the character \u000a gets resolved into a newline character thereby making the rest (x=6) gets shifted to the next line as an executable statement.

 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to compile the following code and it just compiled fine. but i do not understand the meaning of these constants.
1. char c ='\u0061', c1='\61';
2. System.out.println(c + " " +c1); // a 1

1. float f1 =\u0030;
2. System.out.println(f1); // 0.0

how can we assign unicode constants to float?
 
Himai Minh
Ranch Hand
Posts: 1256
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A character is actually a 16-bit integer.

You can assign an integer to a character.
You can also assign a float to an integer. The float has 64-bit. A character has 16-bit.A float has enough space to hold a character.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:A character is actually a 16-bit integer.

You can assign an integer to a character.
You can also assign a float to an integer. The float has 64-bit. A character has 16-bit.A float has enough space to hold a character.


we cannot assign a float to an integer without using casting. secondly a character is a 16-bit signed integer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic