Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 14)
14.
What is the numerical range of a char?
-128 to 127
-(215) to (215) - 1
0 to 32767
0 to 65535
Answer: Option
Explanation:
A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.
Discussion:
19 comments Page 1 of 2.

M. Sheliga said:   9 years ago
As long as you are converting char (16bits) to int (32 bits), the range is 0 to 65535.
If you convert char to short (which requires a cast), the range is -32K to +32K.

public class CharToInt
{
public static void main(String[] args)
{
int i = '\u0000';
System.out.println("bs u 0000 is: " + i);
i = 'A';
System.out.println("A is: " + i);
i = 'B';
System.out.println("B is: " + i);
i = '\uffff';
System.out.println("bs u ffff is: " + i);
short s = (short) '\uffff'; // compiler error without cast
System.out.println("bs u ffff short is: " + s);
s = (short) '\u8000'; // compiler error without cast
System.out.println("bs u ffff short is: " + s);
}
}

-----------------------------------
bs u 0000 is: 0
A is: 65
B is: 66
bs u ffff is: 65535
bs u ffff short is: -1
bs u ffff short is: -32768
(1)

Rahul Kirpekar said:   1 decade ago
Static means when we create object static things create one copy generate for One Class. For (Each class).

Static things means we can access without create object we can access static Variable with class name dot operator and we also can access same methods. ! we also can create class as a static in Inner class. I hope you understand static keyword of JAVA. !

Arun said:   1 decade ago
In C as we can use ASCII characters and to represents all ASCII character 8bits enough hence char size is 1 byte.

In JAVA we can use UNICODE characters which cover worldwide all alphabets sets. The no of Unicode characters is >256 and 1byte is not enough so to represent all characters we have to use 2bytes.

The range for 2bytes is 0 to 65535.

Debasish said:   1 decade ago
Hi Guys,
In character data type they (java creators) only considered the unsigned ( +ve) characters unlike in C like languages it was both signed and unsigned.So in C there was 2 ranges for character and integer(signed and unsigned).

Sagar said:   9 years ago
@Debasish.

In Java all the datatypes are signed, so is character datatype exempted from this rule.

Which I think is correct because ASCII value for any character cannot be negative. Please correct me, if I am wrong.

M.Srikanya said:   1 decade ago
The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). So Char range is 65535.

Divya said:   1 decade ago
Why is the answer not option B?

The range is determines by -2^n to 2^n-1.

Option B includes the -ve range & D doesn't.

Both are correct according to me.

Prathyusha said:   1 decade ago
In C as it occupies 1 byte its range is -128 to 127 but in Java char occupies 2 bytes so its range is 0 to 65535.

Suresh said:   1 decade ago
@Arun said is correct why because java supports UNICODE characters that's why character range is 0 to 65535.

Abu Dhabi said:   9 years ago
@Ieo.

Volatile Keyword is used in synchronization to tell that method is to be shared method.


Post your comments here:

Your comments will be displayed after verification.