incompatible types: java.lang.String cannot be con

2020-04-10 01:29发布

问题:

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.

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

回答1:

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