2

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.

2
  • 2
    because the String s = "Hello" does refer to the class you're defining. If your package is "com.mypackage", s is not a java.lang.String but a com.mypackage.String Commented Oct 14, 2017 at 13:11
  • 8
    Don't name your class String. Just don't. Commented Oct 14, 2017 at 13:12

3 Answers 3

6

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";
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

How does this different from the existing answer?
-1
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);
    }
}

Comments