File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char literal's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char literal Watch "char literal New topic
Author

char literal's

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
public class lapore
{
public static void main(String[]args)
{
a) char ch1 ='\'';
b) char Ch2 ='\uccfe';
c) char Ch3='\u10101';
d) char Ch4='abcd';
}
}
The correct answer's are a and b.
can anybody tell me how do we make sure which value is right and wrong.
I mean what are the rule's to put unicode value's as litera's in char variable.?
What all value's are allowed as char literal's and what all are not.?
Why c and d option's are invalid?
Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
Posts: 82
hi Nitin..
let me try..
a) VALID
because it contains a single apostrophe(') preceded by an escape character which is necessary for the compiler to interpret its correct meaning..such a sequence is called an escape sequence.. otehr escape sequences in java are \b, \n, \t etc..
b) VALID
because a char literal can contain values between \u0000 and \uffff and \uccfe comes within the permissible limit..
c) INVALID
because \u10101 far exceeds the above mentioned limit..
d) INVALID
because 'abcd' isn't a single cahracter.. these are four characters.. if the intention was to provide a unicode sequence, it should have been done as '\uabcd'...
hope this helps..

------------------
Hima


Hima<BR>Sun Certified Java Programmer
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi hima
could u please explain the c option as we u said limit exceeded, what all integer's, we can have in char literal's?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Nitin,
Choice c is incorrect because a character in Java is 16 bits. For a unicode character, each digit (hexidecimal) represents 4 bits. Therefore the unicode character is limited to 4 digits. Choice c has 5 digits which would equate to a 20 bit value to large for a char to hold! Limits are:
'\u0000' to '\uFFFF'
Regards,
Manfred.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
public class lap
{
public static void main(String[]args)
{
char c='\afaf'; //=> char c = 'c';
char d='\u0012';
//System.out.println(c);
}
}
Hi hima,
In the above given programme, i have noticed that we cannot assigh more than 4 alphabet's to the char literal and same is there with the numeric number's.If
i do the thing's as i have shown below then i get a compile time error, as i have excceded four number's.Could u please explain something on that.
char a'\u00123';
char a'\uadcff';

Your previous reply was really helpful.
Indika Perera
Greenhorn

Joined: Feb 28, 2001
Posts: 8
hi, whether you are using Alphebet or numerics it's that Hexadecimal you are representing in these cases.
So, four places can occupy any Hexa digit - from 0(zero) to f.
Thererfore, it's the same reason that you cannot exceed the limit, which Manfred has explained, gives complier error.
Francisco I
Ranch Hand

Joined: Mar 27, 2001
Posts: 44
Originally posted by nitin sharma:
public class lap
{
public static void main(String[]args)
{
char c='\afaf'; //=> char c = 'c';
char d='\u0012';
//System.out.println(c);
}
}

Try char c = '\uafaf' as opposed to char c = '\afaf';
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: char literal's
 
Similar Threads
char value's
Help needed.....
Referring a Local Inner Class
how compiler determines that String literal's length ??
how to find all possible combinations of charcters in string