Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Eclipse for Java syntax checker error for generic arrays
Eclipse for Java syntax checker error for generic arrays [message #1862674] Wed, 20 December 2023 08:57 Go to next message
Tapio Palomäki is currently offline Tapio PalomäkiFriend
Messages: 20
Registered: November 2023
Junior Member
The following line does not compile by hand manually with javac (JDK21), and does not go past Eclipse syntax checker (JDK-17 compat), which claims its a generic array, which is clearly not, very specifically typed expression.

MyClass<Long>[] longsort = new MyClass<Long>[5];

[Updated on: Wed, 20 December 2023 09:01]

Report message to a moderator

Re: Eclipse for Java syntax checker error for generic arrays [message #1864892 is a reply to message #1862674] Fri, 19 April 2024 04:09 Go to previous messageGo to next message
doria avallone is currently offline doria avalloneFriend
Messages: 1
Registered: April 2024
Junior Member
In Java, you cannot directly create arrays of a generic type. This is because generic types are erased at runtime, and the JVM needs to know the specific type that the array will hold to ensure type safety. This code suppresses the unchecked warning because we are manually ensuring that the array will only contain MyClass<Long> instances. It's important to be cautious with this approach to avoid introducing potential ClassCastExceptions at runtime.

MyClass<Long>[] longsort = new MyClass<Long>[5];
@SuppressWarnings("unchecked")
MyClass<Long>[] longsort = (MyClass<Long>[])new MyClass[5];
ArrayList<MyClass<Long>> longsort = new ArrayList<>(5);

Re: Eclipse for Java syntax checker error for generic arrays [message #1864956 is a reply to message #1864892] Mon, 22 April 2024 05:54 Go to previous message
Ziangze Cho is currently offline Ziangze ChoFriend
Messages: 2
Registered: January 2024
Junior Member
Woah, that type mismatch is definitely head-scratching! You're right, it should compile. Here's what might be going on:

JDK Version: While you mentioned JDK21 for javac, sometimes Eclipse can have a different version configured. Double-check both versions to ensure compatibility.
Generic Array Syntax: There might be a quirk with how Eclipse handles generic array syntax. Try the following alternative:
Java
MyClass<Long> longsort[] = new MyClass[5];

This rearranges the brackets to explicitly declare the array type first.

If neither of those suggestions work, try creating a minimal reproducible example and posting it on a Java forum like Stack Overflow. Someone might have encountered the same issue with your specific Eclipse setup.
Previous Topic:keep show settings classpath containers 36%
Next Topic:Selectively redirect only stdout to a file
Goto Forum:
  


Current Time: Thu Jan 23 15:22:52 GMT 2025

Powered by FUDForum. Page generated in 0.02836 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top