Google+

Like Us On Facebook


Total Pageviews

199. Using Character Escape Sequences in String Literals







String literals - A sequence of characters enclosed between a pair of double quotes are identified as String Literals in Java.

Example of a String Literal -  "This is a Sample Text"

Lets use Character Escape Sequences in String Literals.

To start with let use 'New Line' character escape sequence \n in String literals.

Lets implement using \n (New Line) character escape sequence on Eclipse IDE -

1. Launch Eclipse IDE, create a Java Project 'Project 30' as shown below -



2. Create a Java Class 'ClassOne' with main( ) method as shown below -



3. Now lets use the \n character escape sequence to break the characters in the string literal and display them on a new line as shown below -



4. Create a statement to print the String literal stored in the String type variable 'var' as shown below -



5. Save and Run the Java Class 'ClassOne' and observe the String literal is breaked and displayed in a new line in the output where ever the \n is provided in the String literal as shown below -



Lets implement using \t (tab space) character escape sequence  on Eclipse IDE -

1. Create 'ClassTwo' Java Class, use the character escape sequence to break the character in the string literal and display them with a tab space between each and every break, print the string literal and  view the output as shown below -



In the similar way, you can also try the other character escape sequences in the below table -






Please comment below to feedback or ask questions.

The Object Class will be explained in the next post.






No comments:

Blog Index