Java — What is the numerical range of a char (primitive type)?

Java Programming Language Fundamentals Difficulty: Easy
Choose an option
  • A
    -128 to 127
  • B
    -(2^15) to (2^15) - 1
  • C
    0 to 32767
  • D
    0 to 65535
  • E
    Depends on the platform

Answer

Correct Answer: 0 to 65535

Explanation

Introduction / Context:Unlike C/C++, Java's char is a 16-bit unsigned integer type designed to hold UTF-16 code units. Knowing its range is useful when performing conversions and arithmetic involving characters.

Given Data / Assumptions:

  • Java char is fixed-width and unsigned.
  • The question asks for the numeric range.

Concept / Approach:char uses 16 bits and is unsigned, so it ranges from 0 to 2^16 - 1 = 65535. This is independent of platform or CPU architecture (Java's types are platform-independent).

Step-by-Step Solution:

Compute maximum: 2 ^ 16 = 65536.Range is 0 through 65535 inclusive.

Verification / Alternative check:Review the Java Language Specification: char is an unsigned 16-bit integer representing UTF-16 code units.

Why Other Options Are Wrong:

  • -128..127 and signed 16-bit ranges describe byte and short, not char.
  • 0..32767 is 15-bit; char is 16-bit.
  • “Depends on the platform” contradicts Java's fixed type sizes.

Common Pitfalls:Confusing char with C/C++ char (which may be signed/unsigned 8-bit depending on compiler), or equating it with short.

Final Answer:0 to 65535

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion