String data = //some String;
int val = data.charAt(2);
Can val ever have a negative value in any scenario?
String data = //some String;
int val = data.charAt(2);
Can val ever have a negative value in any scenario?
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).
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 typeT
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