aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why char c='\u000a'; is not compiling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why char c= Watch "why char c= New topic
Author

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

prem sagar bojja
Greenhorn

Joined: Dec 27, 2005
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
That character happens to be the newline character, which is not allowed as per the Java Language Specification.


Ping & DNS - my free Android networking tools app
prem sagar bojja
Greenhorn

Joined: Dec 27, 2005
Posts: 8
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

Joined: Aug 16, 2005
Posts: 14153
    
  18

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Aug 16, 2005
Posts: 14153
    
  18

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

Joined: Apr 20, 2006
Posts: 3344

Yes, that's perfect.

Thank you very much Jesper.
Peter Mularien
Author
Ranch Hand

Joined: Sep 06, 2007
Posts: 84
Actually, this could lead to some interesting trick questions, for example:

... this will of course print 6!


Author, Spring Security 3 (the Book), Packt Publishing, 2010
SCJP, OCP
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Mar 13, 2012
Posts: 57
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

Joined: Jul 29, 2012
Posts: 758
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why char c='\u000a'; is not compiling?