简体   繁体   中英

Escape Sequence's inconsistent behavior

I am curious as to why escape sequencing evaluates to different results in different situations.

public class EscapeChars{

    public static void main(String args[]) {
        String sequence = args[0];
        System.out.println(sequence);
    }
}

Input "//\\\\" Output //\\ - This is expected, given that the sequence "\\\\" evaluates to \\

Input "\\\\//" Output \\\\// - Going by my understanding of escape sequencing, I expected the output to be \\// instead.

I want to understand the reason for this apparent inconsistent result.

If you try this code:

System.out.println("\/");

You will get a compilation error( I tested with jdk 1.8 ). The error says: illegal escape character. Which means "/" is invalid. Since you are input the sequence at runtime, so this may be the reason of showing the result as it was inputted.

据我所知,你的 java 程序只是打印由 shell 传递给它的参数,或者任何执行它的程序,所以在打印时,转义序列已经被 bean 处理,所以它不是一个爪哇问题。

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