Converting mismatch char [] to 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 char ac1[] = ac.clone();

throwing an error in eclipse telling me that I cannot convert a char array to Object?

Any reason why this is the case? It was not giving me the same error in the newest version for eclipse, so I wonder if this older version is causing it .

+3


source to share


1 answer


This happens in Eclipse if you have your compiler settings to set up very old source compatibility.

With compatibility level 1.5 or higher, this is fine - but if you set the compatibility level for the source code to 1.3 or 1.4, you will get this error because earlier versions of the Java language specification did not specify what it T[].clone()

returns T[]

.

Text from JLS 1.0 10.7 section:

Array type elements are as follows:

  • [...]
  • Public method clone

    that overrides a method of the same name in a class Object

    and does not override checked exceptions


Equivalent text from Java 8 JLS:

Array type elements are as follows:

  • [...]
  • A public method clone

    that overrides a method of the same name in a class Object

    and does not override checked exceptions. The return type of clone array type T[]

    is T[]

    .

Go to project properties and check if they are using default or project-specific settings and adjust the appropriate settings (either project-specific or workspace settings) to use more modern source compatibility.

I suspect you will find that with your current settings, you will not be able to use generics or other 1.5+ features.

+6


source







All Articles