In character constants, the backslash (\) is a special character, called an escape character, and its function is to escape the next character. The following are some common escape characters:
- \n means a newline character , to change to the beginning of the next line.
- \r represents the carriage return character , which positions the cursor to the beginning of the current line without skipping to the next line.
- \t means a tab character , move the cursor to the position of the next tab character, a tab character is 8 bits.
- \b means backspace , just like the Backspace key on the keyboard.
- \' represents a single quote character , and the single quote is output as it is.
- \'' represents the double quotation mark character , output the double quotation mark as it is.
- \\ represents the backslash character , and the backslash (\) is output as it is.
Examples are as follows:
public class TestVar6{
public static void main(String[] args){
//\n表示换行符,换到下一行的开头
System.out.println("aaabbb");
System.out.println("aaa\nbbb\n");
//\r表示回车符,将光标定位到当前行的开头,不会跳到下一行。
System.out.println("aaabbb");
System.out.println("aaa\rbbb\n");
//\t表示制表符,将光标移动下一个制表符的位置,一个制表符8位。
System.out.println("aaabbb");
System.out.println("aaa\tbbb\n");
//\b表示退格符
System.out.println("aaabbb");
System.out.println("aaa\bbbb\n");
//\'表示单引号字符,将单引号原样输出。
System.out.println("aaa");
System.out.println("\'aaa\'\n");
//\''表示双引号字符,将双引号原样输出。
System.out.println("aaa");
System.out.println("\"aaa\"\n");
//\\表示反斜杠字符,将反斜杠(\)原样输出。
System.out.println("aaa");
System.out.println("\\aaa\\\n");
}
}