不兼容的类型:java.lang.String 不能转换为 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28347247/
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-08-11 06:04:28  来源:igfitidea点击:

incompatible types: java.lang.String cannot be converted to String

javacompiler-errors

提问by Droxx

So basically I'm learning some java (The basics ATM) and I had a few errors but solved them pretty fast. So I got this new weird error that I've never heard about and I researched and found no answers that could help me.

所以基本上我正在学习一些Java(ATM基础知识),我有一些错误,但很快就解决了。所以我遇到了这个我从未听说过的新的奇怪错误,我进行了研究,但没有找到可以帮助我的答案。

I removed the old project and get the old errors on new projects

我删除了旧项目并在新项目中遇到了旧错误

So this is my code:

所以这是我的代码:

import javax.swing.*;

public class Hej {
  public static void main (String[] arg) {
    JOptionPane.showMessageDialog(null, "Hej!");
  }
}

And this is my error on the file that I removed about a month ago:

这是我大约一个月前删除的文件中的错误:

C:\Users\Droxx\Documents\javamapp>javac Hej.java
.\String.java:10: error: incompatible types: java.lang.String cannot be converte
d to String
        r = "Silver ";
            ^
.\String.java:11: error: incompatible types: java.lang.String cannot be converte
d to String
        l = "2";
            ^
.\String.java:13: error: cannot find symbol
        i = r.substring(0,1);
             ^
  symbol:   method substring(int,int)
  location: variable r of type String
.\String.java:17: error: cannot find symbol
                JOptionPane.showMessageDialog(null, "Level:" + a);
                                                               ^
  symbol:   variable a
  location: class String
4 errors

回答by Tim B

This is caused by creating a class called Stringin the same package as the class trying to access java.lang.Stringas pointed out in the comments by Steffan and Stultuske.

这是由于创建了一个String在与尝试访问的类相同的包中调用的类,java.lang.String如 Steffan 和 Stultuske 的评论中指出的那样。