简体   繁体   中英

Type missmatch: cannot convert from java.lang.String to String

I don't understand whats wrong with the following example:

public class String 
{
    public static void main(java.lang.String[] args) 
    {
        String s = "Hello";
    }
}

It says "Type mismatch: cannot convert from java.lang.String to String"

on the String s line. I tried to Import java.lang.String but didn't help.

Why you need to name your class as String ?

Change

public class String 

To something else, like

public class SomethingElse

alternatively use qualified type name for you variable s (but still strongy recommend you not naming your classes like stuff from java.lang package)

java.lang.String s = "Hello";

Change your class name to something else, you cannot use string as your class name.

Java already has a class called String, it creates a conflict.

public class Strings1 {  // class name should not be String

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub


        // String basic program
        // class name should not be saved as String

        String s="Hello";
        System.out.println(s);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM