Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 9)
9.
Which three are valid declarations of a char?
  1. char c1 = 064770;
  2. char c2 = 'face';
  3. char c3 = 0xbeef;
  4. char c4 = \u0022;
  5. char c5 = '\iface';
  6. char c6 = '\uface';
1, 2, 4
1, 3, 6
3, 5
5 only
Answer: Option
Explanation:

(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of a character.

char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always start with a '\u'.

char c4 = \u0022; is wrong because the single quotes are missing.

char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts with '\i' rather than '\u'.

Discussion:
24 comments Page 1 of 3.

Prabhu said:   1 decade ago
What is Unicode?

ASCII which stands for American Standard Code for Information Interchange became the first widespread encoding scheme. However, it is limited to only 128 character definitions. Which is fine for the most common English characters, numbers and punctuation but is a bit limiting for the rest of the world. They naturally wanted to be able to encode their characters too. And, for a little while depending on where you were, there might be a different character being displayed for the same ASCII code. In the end, the other parts of the world began creating their own encoding schemes and things started to get a little bit confusing. Not only were the coding schemes of different lengths, programs needed to figure out which encoding scheme they were meant to be using.

It became apparent that a new character encoding scheme was needed and the Unicode standard was created. The objective of Unicode is to unify all the different encoding schemes so that the confusion between computers can be limited as much as possible. These days the Unicode standard defines values for over 100,000 characters and can be seen at the Unicode Consortium. It has several character encoding forms, UTF standing for Unicode Transformation Unit.

Aruni Mishra said:   1 decade ago
A char literal is represented by a single character in single quotes.

char a = 'a';
char b = '@';

You can also type in the Unicode value of the character, using the Unicode.

Notation of prefixing the value with \u as follows:

char letterN = '\u004E'; // The letter 'N'.

Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). For example, the following are all legal:

char a = 0x892; // hexadecimal literal.
char b = 982; // int literal.
char c = (char) 70000; // The cast is required; 70000 is out of char range.
char d = (char) -98; // Ridiculous, but legal.

And the following are not legal and produce compiler errors:

char e = -29; // Possible loss of precision; needs a cast.
char f = 70000 // Possible loss of precision; needs a cast.

You can also use an escape code if you want to represent a character that can't be. Typed in as a literal, including the characters for linefeed, newline, horizontal tab, Backspace, and single quotes.

char c = '\"'; // A double quote.
char d = '\n'; // A newline.
(1)

Shreya said:   6 years ago
I think its option 1 and 2. You can initialize an integer directly in a char as it represents a number in hexadecimal form. And 2 is correct too as in single quotes we can initialize any character.
(1)

Anil said:   1 decade ago
I tried the above initializations 1,3,6.
But while displaying values, it displays only ? for statements..
Why it displaying like that......

Any one please post the answer for this...

Rahul said:   1 decade ago
char c2 = 'face'.
char can hold only a character.
But here face is complete word.

So to store face we required a String variable.

char c2='z'.
We can store like this .

Kedar said:   1 decade ago
In Java, lower priority data types are converted to its nearest higher priority data types implicitely so that the some of the values of char are also of the int.
(1)

MAHI TEJA.YENUMULA said:   1 decade ago
"UNICODE" is a covertion type in java for ex : in c launguage we have "ASCII" covertion. Remember in java "UNICODE" convertion only takes place.

Basha said:   1 decade ago
@Sandip

c2='a' it is represntaion of character.

But, in the given question c2='face' trying to assign the string value to character variable.

Imsurya said:   9 years ago
Anyone can explain about these two differences?

char c5 = '\iface'; //illegal
char c6 = '\uface'; //legal
(2)

Niharika patidar said:   6 years ago
064770 is an octal representation of the integer value of 27128 then how can we store it in char literal?
(1)


Post your comments here:

Your comments will be displayed after verification.