char

Type Range Default Value
char U+0000 to U+FFFF U+0000

The char type represents a single 16-bit Unicode character.

Literals

A character literal can be created with the `...` syntax, such as:

1
char x = `x`;

Additionally, the char type can use its underlying integer representation.

Integrals without a suffix are interpreted in this order: int, unsigned int, long, unsigned long, byte, signed byte, short, unsigned short, and char. If the integral value does not "fit" within any specified types, such as possible function overloads or a specified variable type, an error will be raised at compile time.

There is no suffix for the char type, and integer literals that fit within the range of the char type can be written without a suffix.

Auto-boxing

All primitive types have a corresponding wrapper class. The corresponding wrapper class for the char type is System.Character.

Auto-boxing occurs when a value of a primitive type is assigned or passed as an argument to a variable or function that expects a value of its corresponding wrapper class. For example, the character value `x` would fit within the range of a char and can be assigned to a variable of type System.Character via auto-boxing:

1
System.Character value = `x`; // Auto-boxing

Without auto-boxing, the equivalent code would be:

1
System.Character value = new System.Character(`x`);

The reverse also occurs. Unboxing is when the wrapper class is automatically converted to its corresponding primitive type. Thus, an instance of System.Character can be unboxed to the char primitive type.

1
char value = new System.Character(`x`); // Unboxing

Conversions for Internal Types

For conversions to and from the char type, consult the Conversions Table.

Conversions for external Type

When converting from external:

  • If the external value is a zero numeric value (including +0, -0, 0.0, and so on), it will be automatically converted to U+0000.

  • If the external value is a non-zero numeric value and fits within the range of the char type, it will be automatically converted to the equivalent character for the numeric character code.

  • If the external value is a non-zero numeric value and is outside the range of the char type, it will be automatically converted to U+0000. Unimplemented

  • If the external value is NaN, it will be automatically converted to U+0000.

  • If the external value is positive or negative Infinity, it will be automatically converted to U+0000. Unimplemented

  • If the external value is a empty string value, it will be automatically converted to U+0000.

  • If the external value is a non-empty one-character string value, it will be automatically converted to the character contained in the string.

  • If the external value is a non-empty multi-character string value, it will be automatically converted to U+0000. Unimplemented

  • If the external value is the Character true value, it will be automatically converted to U+0000.

  • If the external value is the Character false value, it will be automatically converted to U+0000.

  • If the external value is null or undefined, it will be automatically converted to U+0000.

  • If the external value is an array, it will be automatically converted to U+0000.

  • If the external value is an object, it will be automatically converted to U+0000.

  • If the external value is an function, it will be automatically converted to U+0000.

When converting to external, the value will be converted to a JavaScript string value with the string containing the relevant character.

See Also

Share

HTML | BBCode | Direct Link