Characters Datatype in Java

A character variable or constant is declared with the char keyword. A character takes on a single 16-bit Unicode character between single quotes. There are also a number of escape sequences for denoting special characters, as follows:

\t tab

\r carriage return

\n line feed

\f form feed

\b backspace

\" double quote

\' single quote

\\ backslash



In the Circle program, we could add the following declaration immediately after the PI
declaration in line 7:

static final char TAB = '\t';

The statement in lines 16–17 could be replaced by

System.out.println(''A circle of radius " + args[0]
+ “has area of " + TAB + area);


This will add a tab in the output string. We could have simply added \t in the output statement as in

System.out.println("A circle of radius " + args[0]
+ “has area of \t " + area);

No comments:

Post a Comment