错误:不兼容的类型:java.lang.String无法转换为String [英] error: incompatible types: java.lang.String cannot be converted to String

查看:11016
本文介绍了错误:不兼容的类型:java.lang.String无法转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm taking a Java class for college, and was working on some given tasks. This is the code I wrote for it.

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

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

I've tried searching for an answer but I didn't really find anything that helps. Any help is appreciated, thanks.

推荐答案

因为您的类被命名为 String String city 中的非限定类型引用被视为对您自己的类的引用。

Since your class is named String, unqualified type reference in String city is taken as reference to your own class.

将类重命名为一些其他名称,或者你必须写 java.lang.String ,无论你在哪里引用内置Java String class。

Either rename the class to some other name, or you'll have to write java.lang.String wherever you reference the "built-in" Java String class.

这篇关于错误:不兼容的类型:java.lang.String无法转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆