6

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.

1
  • This is really interesting. I have never come across anything like this before. Commented Mar 24, 2013 at 7:03

3 Answers 3

8

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 {
Sign up to request clarification or add additional context in comments.

Comments

2

It seems like you are trying to create a generic class using "String" as the name of your generic type. What generic types do is whenever you create an object from the class, it replaces whatever is in the <> with the new type.

So in your class, if you created a Tree<Integer>, ever instance of "String" in your class would be replaced by "Integer". This is the way some classes such as ArrayList work to allow any type to be used.

Usually when using generic types, a single letter, such as "T", is used to avoid it being the same as a real class type.

So in your case, you are trying to set a "String" that is not actually a String as an actual string.

Comments

0

I solved it by typing

import java.lang.String;

5 Comments

I was getting the same error message with String somefunction() { }. The import removed the error message.
This should not be an answer. java.lang and all sub-packages are auto-imported and should be present.
The error was gone writing import java.lang.String; . I'm not lying.
Well, then there is something bigger wrong with your local setup or classpath. String is part of java.lang, which is the default package available in java, it does not need to be imported. Check the accepted answer, thats the right answer.
java.lang is not auto-imported, not in my case. In tutorials import it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.