Type mismatch convert char[] to an object

I'm sure I'm missing something simple but this problem seems absolutely stupid.

private static void method501(char ac[])
{
    char ac1[] = ac.clone();
}

My problem is that the char ac1[] = ac.clone(); is throwing a type mismatch error in eclipse telling me that I can't convert a char array to an Object?

Any reason why this is? It was not giving me the same error on the newest version of eclipse so wondering if it's just this older version is giving me the problem.

Jon Skeet
people
quotationmark

This happens in Eclipse if you've got your compiler settings to target very old source compatibility.

With a compatibility level of 1.5 or above, it's fine - but if you set the source compatibility level to 1.3 or 1.4, you'll get this error, as early versions of the Java Language Specification didn't specify that T[].clone() returns T[].

Text from JLS 1.0 section 10.7:

The members of an array type are all of the following:

  • [...]
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions

The equivalent text from the Java 8 JLS:

The members of an array type are all of the following:

  • [...]
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

Go into the project properties and check whether it's using the default settings or project-specific settings, and fix the appropriate settings (either the project-specific ones or your workspace settings) to use a more up-to-date source compatibility.

I suspect you'll find that with your current settings you won't be able to use generics or other 1.5+ features either.

people

See more on this question at Stackoverflow