Java 字符串声明 IntelliJ IDEA

Java string declaration IntelliJ IDEA

试图在 Java 控制台应用程序的主要方法中声明一个字符串。

String s = "this is some text";

我收到一条红色下划线,表示预期 'class' 或 'interface'。

如果我把代码改为read

String s = new String("this is some text"); 

一切正常,或者至少代码可以编译。使用 JDK 1.8 并且最近将 IDE 升级到版本 2016.2.4.

这仅在声明新字符串时发生,所有其他类型声明和初始化无需声明新实例即可工作,即

int i = 0;

有人知道为什么第一个声明不起作用吗?

尝试写入控制台时出现类似行为,

System.out.println("this is some text");

单词 'text' 带有红色下划线,表示预期 'class' 或 'interface'。

编辑:整个 class 根据要求

package Sandbox;

public class Main {

    public static void main(String[] args) {
        System.out.println("this fails");
    }
}

然而

package Sandbox;

public class Main {

    public static void main(String[] args) {
        System.out.println(new String("this works"));
    }
}

请参阅下面 IDE 中实际代码的屏幕截图。欢迎评论。

看起来像是 IntelliJ 中的语言注入问题。 https://www.jetbrains.com/help/idea/2016.2/using-language-injections.html

禁用语言注入。那应该可以解决您的问题。

此处描述了 println 方法和字符串的类似问题,并已通过从字符串注入中取消注册 println 来解决:https://intellij-support.jetbrains.com/hc/en-us/community/posts/206836685-System-out-println-hello-analyze-error