Escape Sequences and Format Specifiers Example in java
Write a java Program that illustrates various escape sequences and format specifiers like backspace, hex representation, Carriage return, tab space and more.
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
Program or Solution
class Program
{
public static void main(String args[])
{
// Escape Sequences
// \b back space deletes the previous character
System.out.printf("Hello\b World\n");
// \n New line moves cursor to new line before printing World
System.out.printf("Hello\nWorld\n");
// \t tab space leaves a tab space before World
System.out.printf("Hello\t World\n");
// \r Carriage Return Moves cursor to starting position in the same line so Devil will be Over Written
System.out.printf("Hello World \rDevil\n");
// \" prints double Quotes
System.out.printf("\"Decode School\"\n");
// \\ Prints slash, so a\b
System.out.printf("a\\b\n");
// Format Specifiers
// .2f two digit precision in floating point numbers
System.out.printf("%.2f\n",10/3.0);
// %x hexa representation (a for 10)
System.out.printf("%x\n",10);
// %o Octal representation (12 for 10)
System.out.printf("%o\n",10);
// %b Boolean representation
System.out.printf("%b\n",10);
}
}
Output
Program Explanation
Escape Sequence begins with \
Format Specifier Begins with %
Comments
Related Programs
- Java Program to Print Welcome
- Java Program to Print the given Message
- Java Program to Print the given word
- Java Program to print the given integer number
- Java Program to print the given fractional number
- Java Program to print the given fractional number in 2 digit decimal format
- Java Program to print the ASCII value of a character
- Java Program to print two numbers with a space between them
- Java Program to print two numbers with a tab space between them
- Java Program to print two numbers in two lines
- Java Program to Print the Character of given ASCII Value
- println Statement Example in Java
- Literals in Java With Example
- Print and println difference with Example
- Command Line Argument Example in Java
- Implicit Type Conversion Example in Java
- Explicit Type Conversion Example in Java