简体   繁体   English

Java 字符串声明 IntelliJ IDEA

[英]Java string declaration IntelliJ IDEA

Trying to declare a string in Java inside the main method of a Console application.试图在控制台应用程序的 main 方法中用 Java 声明一个字符串。

String s = "this is some text";

I get a red underline saying, 'class' or 'interface' expected.我得到一个红色的下划线,上面写着“类”或“界面”。

If I change the code to read如果我将代码更改为阅读

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

everything works, or at least the code compiles.一切正常,或者至少代码可以编译。 Using JDK 1.8 and have recently upgraded the IDE to version 2016.2.4.使用 JDK 1.8,最近将 IDE 升级到 2016.2.4 版。

This only occurs when declaring a new String, all other type declarations and initializations work without declaring a new instance, ie这只发生在声明一个新的 String 时,所有其他类型的声明和初始化都可以在不声明一个新实例的情况下工作,即

int i = 0; int i = 0;

Anyone know why the first declaration won't work?有谁知道为什么第一个声明不起作用?

Similar behaviour is exhibited when trying to write to the console,尝试写入控制台时表现出类似的行为,

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

The word 'text' is red underlined saying 'class' or 'interface' expected. “文本”一词是红色下划线,表示需要“类”或“界面”。

EDIT: entire class as requested编辑:按要求全班

package Sandbox;

public class Main {

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

however然而

package Sandbox;

public class Main {

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

See screenshot below of actual code in the IDE.请参阅下面 IDE 中实际代码的屏幕截图。 Comments welcome.欢迎评论。

Looks like an issue with Language Injections in IntelliJ.看起来像 IntelliJ 中的语言注入问题。 https://www.jetbrains.com/help/idea/2016.2/using-language-injections.html https://www.jetbrains.com/help/idea/2016.2/using-language-injections.html

Disable the Language Injections.禁用语言注入。 That should fix your Problem.那应该可以解决您的问题。

An similiar issue with the println method and string is described here and has been solved by unregistering println from string injections: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206836685-System-out-println-hello-analyze-error此处描述了 println 方法和字符串的类似问题,并已通过从字符串注入中取消注册 println 来解决: https : //intellij-support.jetbrains.com/hc/en-us/community/posts/206836685-System-out -println-hello-analyze-error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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