-4
String data = //some String;
int val = data.charAt(2);

Can val ever have a negative value in any scenario?

xingbin
  • 27,410
  • 9
  • 53
  • 103
aquesh
  • 9
  • 1
  • 1
  • 8
  • 6
    This is just a restatement of "can a `char` have a negative value". Read the language reference on the `char` datatype. – Jim Garrison Mar 12 '18 at 07:21
  • Please visit the [help], take the [tour] and read [ask] before posting, to learn how to use this site. Questions that show no research effort and can be easily answered by reading the relevant documentation are considered off-topic. – Jim Garrison Mar 12 '18 at 07:27
  • 1
    Possible duplicate of [negative char Value JAVA](https://stackoverflow.com/questions/15176079/negative-char-value-java) – Ole V.V. Mar 12 '18 at 08:16

1 Answers1

4

No.

char: 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).

Source

According to the documentation on conversions, the conversion from char to int is a widening conversion. That page explicitly states (right above Example 5.1.2-1):

A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.

which basically means that ffff will be padded on the left with zeroes to the integer 0000ffff

CompuChip
  • 9,143
  • 4
  • 24
  • 48