Literals and its Types used in NetBeans part-2: Java

January 22, 2014 , 0 Comments

According to earlier article we have read about literals and some of its types used in Java Programming. We have discussed about the integer literals and floating literals, remaining are in this article.

Boolean Literals

The Boolean type has two values, represented by literals true and false formed from ASCII letters.
A Boolean literals is always of type Boolean. It is either Boolean value true or Boolean value false.

Character Literals

A character literal is one character enclosed in single quotes, e.g., ‘z’. The rule for writing character constant is given below:
A character literal in Java must contain one character and must be enclosed in single quotation marks.
Java allows you to have certain nongraphic characters in character constants. Nongraphic character are those character that cannot be typed directly from keyboard e.g. backspace, tabs, carriage return etc. these nongraphic character can be represented by using escape sequences. An escape sequence is represented by a backslash (\) followed by one or more characters.
Following table gives a listing of escape sequences:

Literals and its Types used in NetBeans: Java

In above table, you see sequences representing \, ‘ , “ and ? also. Though these character can be typed from the keyboard but when used without escape sequence, these carry a special meaning and have a special purpose, however, if these are to be typed as it is, then escape sequence should be used.

The following are the example of char literals:
Literals and its Types used in NetBeans: Java
Unicode escape are processed very early and thus must not be represented through \uHn for sequence that have their unique represented escape sequence e.g. \n or \r etc.

String literals

‘Multiple character’ constants are treated as string literals. The rule for writing string-literals is given below:
A string literals is a sequence of zero or more characters surrounded by double quotes. Each character may be represented by an escape sequence.
A string literals is of class type String. Following are legal string literals:

“abc”                              size 6bytes (each character takes 2 bytes)
“\ab”                               size 4 bytes (\a is one character)
“seema\’s pen”                size 22 bytes
(note: \a and  \’ are escape sequences)

Null Literal

The null type has one value, the null reference, represented by the literals null, which is formed from ASCII characters. A null literal is always of the null type.

Anil Swami

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google