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, 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..
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).
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).
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.
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.
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
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.
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.
OCPJP 6(100 %) OCEWCD 6(91 %)
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.