Digital Stronghold - Software Engineering, Musicality and Facts of Life…



May 6, 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

1 Comment »

The URI to TrackBack this entry is: http://eradicus.blogsome.com/2008/05/06/literals-in-java/trackback/

  1. Comment by Joset Anthony Zamora — May 6, 2008 @ 7:32 am

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Theme designed by Joset Anthony Zamora


Digital Stronghold

↑ Get Headline Animator