错误:不兼容的类型:java.lang.String无法转换为字符串

错误:不兼容的类型:java.lang.String无法转换为字符串,java,Java,我正在上大学的Java课程,正在做一些给定的任务。这是我为它写的代码 public class String { public static void main(String[] args) { String city = "San Francisco"; int stringLength = city.length(); char oneChar = city.charAt(0); String upperCity = city.toUpperCase(); Strin

我正在上大学的Java课程,正在做一些给定的任务。这是我为它写的代码

public class String
{
 public static void main(String[] args)
 {
  String city = "San Francisco";
  int stringLength = city.length();
  char oneChar = city.charAt(0);
  String upperCity = city.toUpperCase();
  String lowerCity = city.toLowerCase();

  System.out.println(city);
  System.out.println(stringLength);
  System.out.println(oneChar);
  System.out.println(upperCity);
  System.out.println();
  }
 }
这就产生了这些结果

C:\Users\sam\Documents\Java>javac String.java
String.java:8: error: incompatible types: java.lang.String cannot be 
converted to String
          String city = "San Franciso";
                        ^
String.java:9: error: cannot find symbol
            int stringLength = city.length();
                                   ^
symbol:   method length()
location: variable city of type String
String.java:10: error: cannot find symbol
            char oneChar = city.charAt(0);
                               ^
symbol:   method charAt(int)
location: variable city of type String
String.java:11: error: cannot find symbol
            String upperCity = city.toUpperCase();
                                   ^
symbol:   method toUpperCase()
location: variable city of type String
String.java:12: error: cannot find symbol
            String lowerCity = city.toLowerCase();
                                   ^
symbol:   method toLowerCase()
location: variable city of type String
5 errors

我试着寻找答案,但没有找到任何有用的答案。感谢您的帮助。

由于您的类名为
String
,因此
String city
中的非限定类型引用将作为您自己类的引用


要么将该类重命名为其他名称,要么在引用“内置”java
String
类的任何位置都必须编写
java.lang.String

系统类java.lang.String与名为String的类之间存在冲突。将类字符串重命名为MyString,即替换行:

public class String


并将包含该类的文件String.java重命名为MyString.java。

不要调用类
String
。将
公共类字符串重命名为其他名称
public class MyString