aspose file tools*
The moose likes Beginning Java and the fly likes char declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "char declaration" Watch "char declaration" New topic
Author

char declaration

Raj Kumar
Ranch Hand

Joined: Sep 19, 2005
Posts: 65
Hi,
check out the code below.

class chartest
{
public static void main(String[] args)
{
char c = '\u000d' ; // Line 5
char c = '\u000a'; // Line 6
char c = '\u0000'; // Line 7
char c = '\uface' ; // Line 8
char c = '\u00001' ; // Line 9
char c = '\101'; // Line 10
char c = 65; // Line 11
char c = '\1001' ; // Line 12
}
}

compiler error :
-----------------------------------------------------------------------
chartest.java:5: illegal line end in character literal
char c = '\u000d' ;
^
chartest.java:6: illegal line end in character literal
char c = '\u000a';
^
chartest.java:9: unclosed character literal
char c = '\u00001' ;
^
chartest.java:9: unclosed character literal
char c = '\u00001' ;
^
chartest.java:12: unclosed character literal
char c = '\1001' ;
^
chartest.java:12: unclosed character literal
char c = '\1001' ;
^
----------------------------------------------------------------------


pls explain the exact and an easier way to identify the correct initialization of char.

thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
char c = 0xd;

and so on... For characters for defined escape sequences you can use them:

char c = '\n';
[ February 24, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Raj Kumar:
Hi,
check out the code below.

class chartest
{
public static void main(String[] args)
{
char c = '\u000d' ; // Line 5
char c = '\u000a'; // Line 6
char c = '\u0000'; // Line 7
char c = '\uface' ; // Line 8
char c = '\u00001' ; // Line 9
char c = '\101'; // Line 10
char c = 65; // Line 11
char c = '\1001' ; // Line 12
}
}

compiler error :
-----------------------------------------------------------------------
chartest.java:5: illegal line end in character literal
char c = '\u000d' ;
^
chartest.java:6: illegal line end in character literal
char c = '\u000a';
^
chartest.java:9: unclosed character literal
char c = '\u00001' ;
^
chartest.java:9: unclosed character literal
char c = '\u00001' ;
^
chartest.java:12: unclosed character literal
char c = '\1001' ;
^
chartest.java:12: unclosed character literal
char c = '\1001' ;
^
----------------------------------------------------------------------


pls explain the exact and an easier way to identify the correct initialization of char.

thanks


In line 5, you are trying to create a character literal using the code for carriage return, and in line 6, you are trying to create a character literal using the code for linefeed.

You are not allowed to create character literals with those values.

Instead you use \r and \n.

In line 9, you aren't using a hexadecimal number.

When you use a literal of the form \xxx, you are specifying the char using it's octal representation. The valid range of characters in octal is 000 to 377.

Here are a couple of resources.

http://tecfa.unige.ch/guides/java/langspec-1.0/3.doc.html#100960

http://www.janeg.ca/scjp/lang/charLiteral.html
 
Don't get me started about those stupid light bulbs.
 
subject: char declaration
 
Similar Threads
char assignments
dan exam dought
Compile time Difference between // and /* */
unicode character
why java compiler compile comments?