Would I be correct if I thought the character was converted to its
ASCII value for the purposes of this math operation?
Yes (except you need to change your first line to String str = "foo";
. Basically char
is an integral type which can be used in arithmetic expressions.
Javadoc says:
charAt
public char charAt(int index)
Returns the char value at the
specified index.
JLS (Java Language Specification) says:
Chapter 4. Types, Values, and Variables
The numeric types are the
integral types byte
, short
, int
, long
, and char
and
4.2. Primitive Types and Values:
char
, whose values are 16-bit unsigned integers representing UTF-16
code units (§3.1)"
and
4.2.1. Integral Types and Values
For char, from '\u0000'
to '\uffff'
inclusive, that is, from 0
to 65535
and
5.6.2. Binary Numeric Promotion
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
...
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
...if either operand is of type long, the other is converted to long.
and
15.18.2. Additive Operators (+ and -) for Numeric Types
The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.
Binary numeric promotion is performed on the operands (§5.6.2).