Tuesday, May 06, 2008

Literals in Java

Literals are values found in the source code and are known at compile time.


[boolean literals]


Each boolean type can only hold a literal true or a literal false. Boolean types can not hold numbers unlike in C/C++.



true
false

[char literals]


A char literal can be represented by a single character enclosed in single quotes.



char netPacket = 'K';

Another valid representation is in the form of a Unicode.



char netPacket = '\uCAFE';

The following are also valid, these are special characters represented by using escape sequences.


new line



'\n'

tab



'\t'

backspace



'\b'

form feed



'\f'

carriage return



'\r'

single quote



'\''

double quote



'\"'

backslash



'\\'

[integral literals]


Integral literals can be represented in 3 ways, octal, decimal, hexadecimal. Octal representations are preceded by a 0. Decimal representations contain no prefixes / suffixes. Hexadecimal representations are prefixed with 0x.


decimal



814

octal



031

hexadecimal



0xCAFEBED

[floating-point literals]


Floating-point literals are represented by floating-point numbers. A floating-point number must have one of the following:


decimal point (.)



8888.8888

scientific notation (e / E)



8.88E+8

suffixes (d / D for double & f / F for float)



8F, 8.8D

No comments: