Running into this? Syntax error, insert “Dimensions” to complete ReferenceType
This error usually occurs when you use a primitive type like int or boolean inside a generic declaration, such as with a List, Map, or Function. Since generics expect class types, using a primitive leads to a confusing compiler response.
The solution? Use a wrapper class like Integer, Boolean, Long, Double, or Character instead of their primitive counterparts.
What Triggers the Error?
int, boolean, long, double, and char are all primitive types. When you use one of these inside a generic declaration, the Java compiler doesn’t just reject it, but misinterprets your intent.
For instance, when the compiler sees something like List<int>, it tries to parse int as a ReferenceType, since that’s what generics expect. But int isn’t a valid reference. Instead of stopping there, the parser assumes you’re trying to declare an array type like int[].
Since the brackets are missing, it throws the error: Insert Dimensions to complete ReferenceType. The fix has nothing to do with array dimensions.
How To Fix the Error with Correct Type Usage?
Now that you know what causes the error, here are a few common mistakes that could trigger it, and how correcting the type resolves it.
1. Using boolean in a generic declaration
Function<List<Integer>, boolean> contains =
(list, value) -> list.contains(value);
In this code, you’re trying to define a Function that takes a list of integers and returns a boolean. The intention is valid, but the generic declaration expects an object type for the return value, not a primitive like boolean. To fix it, change the return type from boolean to Boolean.
2. Using int as a value in Map
Map<String, int> ageMap = new HashMap<>();
Here, you’re declaring a map with String keys and int values. But int is a primitive, and generics only work with objects. The compiler gets confused and thinks you’re trying to use an array type like int[], which is where the “Dimensions” error comes from.
3. ArrayList with long
List<long> ids = new ArrayList<>();
In this case, you’re creating a list meant to hold long values. But again, long is a primitive, which generics don’t accept directly. The compiler doesn’t know what to do with long in this context. The fix is to use Long, which is the wrapper type.
Wrapping Up
Whenever you’re working with generics like List<T>, Map<K, V>, Function<T, R>, and so on, just pause for a second and ask: Is this a primitive type? If it is, swap it out with its wrapper class. That simple habit will save you from running into this error over and over again.
If you’re still unsure, just remember that if it doesn’t have methods, it’s probably not the right fit for a generic type.