Type mismatch error between java.lang.String and String

StumpedCoder picture StumpedCoder · Mar 24, 2013 · Viewed 12.6k times · Source

This is really an odd problem, I've never encountered something like it. The following is a helper class I have declared inside a larger class. It runs perfectly fine as is:

private static class Tree<String> {
    private Node<String> root;

    public Tree(String rootData) {
        root = new Node<String>();
        root.data = rootData;
        root.children = new ArrayList<Node<String>>();
    }

    private static class Node<String> {
        String data;
        Node<String> parent;
        ArrayList<Node<String>> children;
    }
}

However, by messing around I discovered a confounding problem. By replacing this line:

root.data = rootData;

with:

root.data = "some string literal";

I get this error:

Type mismatch: cannot convert from java.lang.String to String

I tested out some string literal assignments in other classes and it appears to work perfectly fine. I recently updated to Java 1.7.0_15 and recently downloaded and installed Eclipse 4.2. I suspect there might be some confusion in the build path or something. Using Ubuntu 12.04

Any help would be greatly appreciated! I've searched and searched and couldn't find anything close to this problem.

Answer

j__m picture j__m · Mar 24, 2013

You're creating a new generic type parameter named "String", which is probably not what you want.

Change

private static class Tree<String> {

to

private static class Tree {