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.

public class Hello { public static void main(String args[]) { //Write your code here } }

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

Escape Sequences and Format Specifiers Example in java Output

Program Explanation

Escape Sequence begins with \

Format Specifier Begins with %


Comments