Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Can you cast a String to a char by typing:
String z = "A";
char zz = (char)z;
please help. I get the "inconvertible types" error

That sort of thing won't work in most any language. A string is an array numbers (just known as chars in this case), and a char holds just one number, so they're not compatible. It's no different than trying to cast an array of integers (or any group of integers), to a single integer. Which integer should be used?
In your example, you and I both can see that z only contains a single character, but there's no simple way for the compiler to know z will always contain just one. It only knows that z is an array type, that can hold anywhere from zero to millions of characters.
You can, however, grab a specific char from the string. For example:
char zz = z.charAt(0);

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |