Insert Dimensions to complete Expression/ReferenceType
Asked Answered
S

7

50

I'm a newbie to Java.

I have provided a short snippet from my code for BFS.

public int bfs(Person p, Person q) {
    private HashMap<Person, boolean> marked;
    private int count;

    marked = new marked<Person, boolean>();
    count = new int;
}

According to Eclipse, I have an error on each of the last 4 lines.

Syntax Error: insert "Dimensions" to complete expression/referencetype.

I would appreciate any input/advice!

Stone answered 19/1, 2016 at 19:34 Comment(2)
Don't make bad habits. In future, don't ever put 'JAVA' anywhere in your question. For some reason, this is a trend amongst bad questions - don't make people think this is one. Just use 'Java' or leave it out considering the Java tag makes it obvious this is a Java question.Boding
You'd better read carefully Eclipse's diagnostics and take them as a guide to find the solutions. A tutorial on Java will be useful too.Claudell
A
89

Cause of this error -You are trying to pass a primitive object into a generic type declaration whereas generic types always expect a Wrapper Class object. So please use 'Boolean' instead of 'boolean' in your code i.e. 'B' in caps.

Amalgamate answered 2/9, 2017 at 19:35 Comment(3)
Generic type, delimited by <>(angle brackets), can be any class type, any interface type, any array type, or another type variable. As long as the generic type is non-primitive, it's good to go. If you want to read more, the official documentation is herePlain
What does "Dimension" signify?Aviary
Correct. Use wrapper class objects ex:- Integer instead of int or Int.Atrice
R
14

You need to use the wrapper object not the primitive. Use Boolean instead of boolean.

Raquelraquela answered 19/7, 2017 at 9:16 Comment(0)
L
6

Satyendra Sharma's answer is absolutely correct, but here's some reasoning of what exactly the error message is saying.

The error is caused by using a primitive type, which cannot be used as a generic type argument. For instance, List<boolean> is incorrect, whereas List<Boolean> is correct. Wrapper classes can be used to wrap the primitive values and yield a reference type, which can be used with generics.

Insert dimensions? What?

The message "Insert dimensions to complete expression/referenceType" is probably because in order for the expression to become valid, the only valid token here is a set of square brackets.

For instance,

HashMap<Person, boolean[]> marked;

will just compile fine. This is because, unlike a boolean, a boolean[] is an object.

Laryngotomy answered 28/9, 2020 at 12:28 Comment(0)
F
1

Generics are resolved during compile time and during runtime there's no context about the generics used in your code. The Object is then type-cast into the class type provided against the generic type. Now both primitive and object are completely unrelated entities in java. Direct type-cast of Object to a primitive isn't possible in java. For this reason, the use of primitive type in generic is disallowed and eclipse gives this warning.

Foote answered 20/6, 2018 at 8:56 Comment(0)
O
0

First I would suggest you start reading a Java tutorial...

https://docs.oracle.com/javase/tutorial/java/TOC.html

For your issues specifically:

As for your code, you can initialize your variables right when you declare them:

    Map<Person, Boolean> marked = new HashMap<Person, Boolean>();
    int count = 0; // or whatever initial value
Optometer answered 19/1, 2016 at 19:52 Comment(0)
B
0

It seems that this snippet is throwing around random keywords without any understanding - I would suggest a Java tutorial. First of all, generics are one of the main uses for boxing. boolean or any other primitives (you can recognise these by the fact that their identifiers are in lower-case and most IDEs will highlight them) cannot be used as a generic type, and their capitalised equivalent must be used (a simple wrapper class). Here, use HashMap<Person, Boolean>.

I'm not sure what is meant by marked = new marked... - clearly, marked is not a type and cannot be used in this context. new x(params) initialises an object of type x, passing its constructor params. new x<generics>(params) is the same but the generic type(s) of x are generics.

Finally, new int is not at all valid - see my explanation above. Primitives are not objects, which means initialising them is meaningless and therefore invalid. Also, what do you expect this expression to yield? Something of type int, but you are not specifying which int. The correct syntax is a literal: count = x; where x is some integer within the range of int.

As a side note, your method has an unclear name and variables may be initialised in the same line you declare them to simplify code.

Boding answered 19/1, 2016 at 19:57 Comment(0)
R
0

Visit Cannot Instantiate Generic Types with Primitive Types

Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.

The type parameter, V, actually also K, which is declared in HashMap<K,V>, will be replaced with Object after erasing, because they are unbounded. While primitive type can not be store as Object.

Razid answered 29/1, 2019 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.