Home >Java >javaTutorial >What are the functions of \t,\n,\r,\b,\f in java

What are the functions of \t,\n,\r,\b,\f in java

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-05-13 09:43:053030browse

\t,\n,\r,\b,\f The role of

You will know by looking at the direct output

System.out.println("11111\ t2225222");System.out.println("55555\n665666");System.out.println("88877\r5454421\n");System.out.println("777458\b6594954");System.out.println ("777788\f6594954");

Output result:

What are the functions of \t,\n,\r,\b,\f in java

Conclusion

  • \t Equivalent to the tab key

  • \b Backspace

  • \ f The console output is equivalent to the following figure

  • \n Newline

  • \r Enter

Enter\r The original meaning is to return the cursor to the beginning of the line. The English return of r, the control character can be written as CR, that is, Carriage Return

line break \n The original meaning is that the cursor goes to the next line (not necessarily to the beginning of the next line). n's English newline, the control character can be written as LF, that is, the difference between Line Feed

\n\r\t\f

Generally summarize the functions of \n \r \t \f

##\nLine break \rEnter\tTab (equivalent to tab)\fPage change
Character Function
Explain the characteristics of each individually.

\n Newline character, positioning the cursor to the next line.

public class Test {
	public static void main(String[] args) {
		System.out.print("aaaaaaaaaaaaa\nbbbb");
	}
}

Result:

What are the functions of \t,\n,\r,\b,\f in java

\r The carriage return character returns the cursor to the beginning of the current line. If there was content on the line before, it will be overwritten;

public class Test {
	public static void main(String[] args) {
		System.out.println("hello world\r12345");
	}
}

The compiler output is:

What are the functions of \t,\n,\r,\b,\f in java

Here, it looks like After the previously output hello world was cleared, the same output as 1234 was returned. It's different from its original meaning. Shouldn't it be outputting "1234 world"?

However, the output result in the console is the result we expected:

What are the functions of \t,\n,\r,\b,\f in java

As for the reason, it should be related to the compiler.

\t is the tab character. Equivalent to tab indentation.

It will make the content you output be a multiple of 4. If the string "a\t" is output, then 3 spaces will be output after a. If the string aaaa\ is output, t, then four spaces will be output after 4 a’s are output. So what if it outputs aaaaa\t? , it will output 3 spaces after it, so that the number of characters output is exactly a multiple of 4.

The following is the test code;

public class Test {
	public static void main(String[] args) {
		System.out.println("a\t*");
		System.out.println("123412341234");
		System.out.println("aaa\t*");
		System.out.println("123412341234");
		System.out.println("aaaa\t*");
		System.out.println("123412341234");
		System.out.println("aaaaa\t*");
		System.out.println("123412341234");
	}
}

The result:

What are the functions of \t,\n,\r,\b,\f in java

\f is the form feed character, output on the console Pointless.

Demonstrate here:

public class Test {
	public static void main(String[] args) {
		System.out.println("aaaa\fbbbb");
	}
}

Result:

What are the functions of \t,\n,\r,\b,\f in java

The above is the detailed content of What are the functions of \t,\n,\r,\b,\f in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete