Friday, October 27, 2017

Using Escape Character / Sequences or Special Characters

Disclaimer: If you find any inaccuracy in this article, please let me know how to improve it further.
==================================


List of all escape sequences is available at the bottom of https://docs.oracle.com/javase/tutorial/java/data/characters.html
One of those is "\t";

System.out.println("\t");
or,
System.out.println('\t');

The line above mimics the behavior of the "tab key" present on the keyboard. So, theywork in exactly the same way.

Tab key moves the cursor to the next "tab stops". Tab stops are the places where cursor jumps to, when the tab key is pressed.

In word processing applications such as LibreOffice Writer, you can set the locations or gaps for tab stops. Similarly, remember that we change "indentation level" in Dr Java IDE,  to 5 spaces (instead of 2), that tells amount of spaces the cursor will move when tab is pressed.

By default (since its invention), tab character moves 8 spaces, which is roughly an inch.
This behavior will be visible for the line of text that you print in java or write in notepad or word.

When you are writing some character, you are decreasing the space to be jumped.


For example,
if you only type "tab" and write "hello", it will give 8 spaces and "hello" will appear at 9th position.
If you write "winter", then "tab", then "bye", tab moves the cursor 2 spaces to reach 9th position.
If you write "winterA", then "tab", then "bye", tab moves the cursor 1 space to reach 9th position.
If you write "winterAB", then "tab", then "bye", tab moves the cursor 8 spaces to reach 17th position.

The lines containing hyphens and numbers below are for ease of demonstration only. The output should like as follows:

----\t--
12345678901234567890
        hello
winter  bye
winterA bye
winterAB        bye
12345678901234567890


In short, tab will move the cursor AFTER column positions that are multiples of 8. That is 9, 17, 25 ...... (8n+1) where n = 1, 2, ....

So, number of spaces to be jumped can vary from 1 to 8, depending on how many columns are occupied by previous text in the same line.

In some environments, instead of 8, it becomes 2 or 4 or 5 or 6 etc.
In those cases, tab stops will be at (2n+1) or (4n+1) or (5n+1) or (6n+1) etc.

Find out the behavior of your environment by copying and pasting following lines to the "interaction pane" of DrJava,

System.out.println("----\\t--");
System.out.println("12345678901234567890");
System.out.println("\thello");
System.out.println("winter\tbye");
System.out.println("winterAB\tbye");
System.out.println("12345678901234567890");


References:
1) http://en.wikipedia.org/wiki/Tab_stop
2) http://en.wikipedia.org/wiki/Tab_key#Usage
3) http://en.wikipedia.org/wiki/LibreOffice_Writer
4) http://www.coderanch.com/t/392682/java/java/tabs-spaces
5) http://cboard.cprogramming.com/c-programming/41287-horizontal-tab-%5Ct-what-determines-number-spaces.html
6) http://mathbits.com/MathBits/Java/Introduction/escapesequences.htm
7) https://docs.oracle.com/javase/tutorial/java/data/characters.html


If you know how to run java programs from commandline/console, then try printing \007 as well if you want to print something more awesome.

Don't forget to follow the steps :
https://web.archive.org/web/20140506231020/http://www.sadafnoor.com/blog/print-sound-java/

Save the program in My computer, directly inside C or D drive or any other drive
For example, as C:\testBeep.java
Then open and compile using DrJava
From Start menu -> Run, type cmd and click ok
From the black cmd / "command prompt" window,
type the following
java testBeep
It should make a beep sound.

No comments: