I'm currently struggling to understand why this expression
String test = new String("hello");
char y = test.charAt(0) - '0';
is not counted as compile-time constant expression and yields the error
error: incompatible types: possible lossy conversion from int to char
char y = test.charAt(0) - '0';
but this expression does compile
String test = new String("hello");
char[] charArr = test.toCharArray();
char y = (char) (charArr[0] - '0');
As my current understanding right now upon defining the String variable I've already declared a compile-time variable. I am simply just retrieving the character at the specified index of test, then do some arithmetic operation on it.
Is this caused by the inner working of the String.charAt(index) function? Or is it because of the Java language feature? Or it is caused entirely by something else? May I have some clarification on this or further reading resources? Thank you.