Here is the original question: If x is a char, and values is an int array, then values[x]
The answer is that x will be casted automatically as an int due to the int array. The casting will be done based on X's Ascii value.
Now normally casting this would cause an error, but since x can be casted as a value in the int array, does this also mean if x was casted as a boolean(is this possible as an array?) or another type such as a double, it would be based off of the array that x is casting as?
So if I have values as a double array, x would be casted as a double? It says normally that an array's values must be an int value but does this also apply to arrays of different types?
I can't be sure from what you've written but I think you're confusing the type of values[x] and the type of x.
Array indices can only be expressions that produce non-negative integer values. There is no such element as values[1.5].
Therefore, in your example, x can only be an integer type. Since char is an integer type, then the compiler knows to treat it as an integer value when it's used as an index to the values array. This is where the ASCII value comes into play. The char 'A' has an integer value of 65, so if you have this:
The above code assumes that the values array has at least 66 elements in it. What type those elements are is independent of x. The above code will work with any one of the following declarations for values, but of course you can't have all of them at the same time:
Jenny Flair wrote:The answer is that x will be casted automatically as an int due to the int array. The casting will be done based on X's Ascii value.
I don't think it's correct to say x will be "casted automatically as an int due to the int array." I think this is the starting point of your confusion and it gets worse as you go further. Again, the type of values[x] has absolutely nothing to do with the type of the array index x.
What I think happens is automatic promotion to int, again because the char type is already an integer type in Java so there's no point in casting it. Since indices have to be int values, the char will be automatically promoted to its equivalent int value. That is, char 'A' will be promoted to int 65 and char ' ' (space character) will be promoted to its equivalent int 32.
You can use http://www.asciitable.com/ to see the numeric values of the first 127 characters. Apart from TAB (\t, 9), LF (\n, 10) and CR (\r, 13), nothing before space (32) is of much interest for most people.