java.lang.String 和 String 之间的类型不匹配错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15595946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:11:47  来源:igfitidea点击:

Type mismatch error between java.lang.String and String

javastringtypesvariable-assignmentmismatch

提问by StumpedCoder

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

我在其他类中测试了一些字符串文字赋值,它似乎工作得很好。我最近更新到 Java 1.7.0_15,最近下载并安装了 Eclipse 4.2。我怀疑在构建路径或其他方面可能存在一些混乱。使用 Ubuntu 12.04

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

任何帮助将不胜感激!我已经搜索并搜索过,但找不到与此问题接近的任何内容。

回答by j__m

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

您正在创建一个名为“String”的新泛型类型参数,这可能不是您想要的。

Change

改变

private static class Tree<String> {

to

private static class Tree {

回答by Jsdodgers

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.

您似乎正在尝试使用“String”作为泛型类型的名称来创建泛型类。泛型类型的作用是,无论何时从类创建对象,它都会用<>新类型替换 中的任何内容。

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.

因此,在您的班级中,如果您在班级中创建了Tree<Integer>“String”的任何实例,则将被“Integer”替换。这是某些类(例如 ArrayList)允许使用任何类型的工作方式。

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

通常在使用泛型类型时,使用单个字母,例如“T”,以避免它与真正的类类型相同。

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

因此,在您的情况下,您试图将实际上不是字符串的“字符串”设置为实际字符串。

回答by Learning

I solved it by typing

我通过输入解决了它

import java.lang.String;