programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Different Object Type in an Array

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Hello Javaranchers,

I am curious as to how this answer was gotten.

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?

Saloon Keeper
Posts: 9720
80
• Number of slices to send:
Optional 'thank-you' note:
The index has to be an int. The array type can be anything.

Sheriff
Posts: 17306
299
• 1
• Number of slices to send:
Optional 'thank-you' note:
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:

Junilu Lacar
Sheriff
Posts: 17306
299
• Number of slices to send:
Optional 'thank-you' note:

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.

Sheriff
Posts: 22701
129
• Number of slices to send:
Optional 'thank-you' note:
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.

 "I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising