Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char literal's

 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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';
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic