📜  Java的转义序列

📅  最后修改于: 2021-05-20 05:33:06             🧑  作者: Mango

一个字符以反斜杠(\)之前它是一个转义序列或转义字符。我们使用转义字符来执行某些特定任务。 Java的转义序列或转义字符总数为8。每个转义字符都是有效的字符字面量。
Java转义序列列表:

为什么我们需要转义序列?
假设我们要运行后续的Java代码:

public class Test {
    public static void main(String[] args)
    {
        System.out.println("Hi geek, welcome to " GeeksforGeeks ".");
    }
}

这段代码给出了一个编译时错误:

prog.java:3: error: ')' expected
    System.out.println("Hi geek, welcome to "GeeksforGeeks".");
                                             ^
prog.java:3: error: not a statement
    System.out.println("Hi geek, welcome to "GeeksforGeeks".");
                                                          ^
prog.java:3: error: ';' expected
    System.out.println("Hi geek, welcome to "GeeksforGeeks".");
                                                             ^
3 errors

发生这种情况是因为编译器只希望引号内的字符串,而当编译器找到引号时,它希望在不久的将来有另一个引号(结尾的引号),并且应该在它们之间创建文本字符串。在这种情况下,单词“ GeeksforGeeks”的引号会嵌套(在另一个引号内)。一旦编译器到达此处,编译器就会感到困惑。根据规则,引号建议编译器创建字符串,但是编译器之前忙于执行该操作,并且代码给了我们编译时错误。

因此,我们应该向编译器提供有关引号的正确说明。即,当使用引号创建字符串(作为命令),以及引号是字符本身时(输出字符串的一部分)。
其他字符也会引起类似的混乱(例如反斜杠(),单引号和双引号(’,)),并且在每种情况下,这些错误也会导致编译时错误。为了解决这些问题,我们必须使用Java字符转义。

控制顺序:
控制序列不是什么,但是与字符(必须转义的字符)粘合的反斜杠(\)称为控制序列。
例子:
\\是用于显示反斜杠作为输出的控制序列。

因此,让我们在之前的Java代码中使用此概念来避免编译时错误:

public class Test {
    public static void main(String[] args)
    {
        System.out.println("Hi geek, welcome to \"GeeksforGeeks\".");
    }
}
Output: Hi geek, welcome to "GeeksforGeeks".

Java转义字符的一些编码示例

  1. 转义序列\ t的Java代码:
    // \t -> It gives a tab between two words.
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning\t Geeks! ");
        }
    }
    

    输出:

    Good Morning     Geeks!  
  2. 转义序列\ b的Java代码:
    // The escape sequence \b is a backspace character
    // It moves the cursor one character back with
    // or without deleting the character(depending upon compiler)
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning\bg Geeks! ");
        }
    }
    

    输出:(输出取决于编译器)

    Good Morning Geeks! 
     
  3. 转义序列\ n的Java代码:
    // This \n escape sequence is for a new line.
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning Geeks! \n How are you all?  ");
        }
    }
    

    输出:

    Good Morning Geeks! 
    How are you all? 
    
  4. 转义序列\ r的Java代码:
    // This \r escape sequence is a carriage return character 
    // It moves the output point back to the beginning of the line without moving down a line (usually).
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning Geeks! \r How are you all?  ");
        }
    }
    

    输出:(输出取决于编译器)

    Good Morning Geeks! 
      How are you all? 
    
  5. 转义序列\ f的Java代码:
    // This \f escape sequence is a form feed character
    // It is an old technique and used to indicate a page break.
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning Geeks! \f How are you all?  ");
        }
    }
    

    输出:(输出取决于编译器)

    Good Morning Geeks!  
                         How are you all? 
     
  6. 转义序列的Java代码’
    // This \' escape sequence is for printing a single quotation mark on the text string
      
    public class Gfg {
        public static void main(String[] args)
        {
            System.out.println("Good Morning \'Geeks!\' How are you all?  ");
        }
    }
    

    输出:

    Good Morning 'Geeks!' How are you all? 
  7. 转义序列的Java代码\”
    // This \" escape sequence is for printing a double quotation mark on the text string
      
    public class Gfg {
        public static void main(String[] args)
        {
            System.out.println("Good Morning \"Geeks!\" How are you all?  ");
        }
    }
    

    输出:

    Good Morning "Geeks!" How are you all? 
  8. 转义序列的Java代码\\
    // This \\ escape sequence is for printing a backslash on the text string
      
    public class Gfg {
        public static void main(String[] args)
        {
            System.out.println("\\- this is a backslash. ");
        }
    }
    

    输出:

    \- this is a backslash. 

    说明:它包含两个反斜杠,这意味着在读取第一个\编译器后,将下一个\作为新字符读取。