2 Java Language Fundamentals
2 Java Language Fundamentals
FUNDAMENTALS
BASIC LANGUAGE ELEMENTS
Like any other programming language, Java is
defined by:
Grammar rules specify the Syntax formation
Semantic Definition that specifies the meaning of
legal syntaxes
BASIC LANGUAGE ELEMENTS:
LEXICAL TOKENS
The low level language elements are called
Lexical Tokens, or just Tokens.
They are the building blocks for more complex
constructs.
Identifiers, numbers, operators, and special
characters, are all examples of tokens.
They can be used to build high level constructs
like expressions, statements, methods, and
classes.
BASIC LANGUAGE ELEMENTS:
IDENTIFIERS
A name in a program is called as an identifier.
They can be used to denote classes, methods,
variables, and labels.
An identifier is composed of a sequence of characters.
Each character can be either
a letter,
a digit,
a connecting punctuation (such as an underscore _), or
any currency symbol.
BASIC LANGUAGE ELEMENTS:
IDENTIFIERS: RULES
The first character in an identifier cannot be a digit.
Identifiers in Java are case sensitive, for example, price
and Price are two different identifiers.
Examples of Legal & Illegal Identifiers:
Note that the decimal point and exponent are optional and
that at least one digit must be specified.
BASIC LANGUAGE ELEMENTS:
LITERALS: BOOLEAN LITERALS
The primitive data type boolean represents the truth values
true or false.
these values are denoted by the reserved literals true or
false respectively.
BASIC LANGUAGE ELEMENTS:
LITERALS: CHARACTER LITERALS
A character literal is quoted in single quotes (‘).
All character literals have primitive data type char.
Characters in Java are represented by the 16-bit Unicode
character set.
In the table, note that digits 0-9, upper case letters A-Z, and
lower case letters a-z have contiguous Unicode values.
Any Unicode character can be specified as a four digit
hexadecimal number (i.e. 16 bits) with the prefix ‘\u’
BASIC LANGUAGE ELEMENTS:
LITERALS: CHARACTER LITERALS
Examples of Character Literals
Character Literal Character Literal Using Unicode Value Character
‘ ’ ‘\u0020’ Space
‘0’ ‘\u0030’ 0
‘1’ ‘\u0031’ 1
‘9’ ‘\u0039’ 9
‘A’ ‘\u0041’ A
‘B’ ‘\u0042’ B
‘Z’ ‘\u005a’ Z
‘a’ ‘\u0061’ a
‘b’ ‘\u0062’ b
‘z’ ‘\u007a’ z
‘å’ ‘\u008c’ å
BASIC LANGUAGE ELEMENTS:
LITERALS: ESCAPE SEQUENCES
Character combinations consisting of a backslash (\)
followed by a letter or a combination of digits are called as
escape sequences.
Certain escape sequences define special character values.
These can be single quoted to define character literals.
For example, character literals ‘\t’ and ‘\u0009’ are
equivalent
BASIC LANGUAGE ELEMENTS:
LITERALS: ESCAPE SEQUENCES
Examples of Escape Sequences
Escape Sequence Unicode Value Character
\b \u0008 Backspace
\t \u0009 Horizontal Tab or Tab
\n \u000a Linefeed or New Line
\f \u000c Form Feed
\r \u000d Carriage Return
\’ \u0027 Apostrophe Quote
\’’ \u0022 Quotation Mark
\\ \u005c Backslash
Note: The character literals ‘\u000a’ & ‘\u000d’ should not be used to
represent newline and carriage return in the source code.
• They are interpreted as line terminating characters by the compiler and
will cause compile time errors.
BASIC LANGUAGE ELEMENTS:
LITERALS: ESCAPE SEQUENCES
We can also use escape sequence \ddd to specify a character literal by octal
value.
Each ‘d’ can be an octal digit (0-7).
The number of digits should be three or fewer, and the octal value cannot
exceed \377 i.e., only the first 256 characters can be specified with this
notation.
Examples of Escape Sequences
‘\141’ ‘a’
‘\46’ ‘&’
‘\60’ ‘0’
BASIC LANGUAGE ELEMENTS:
LITERALS: STRING LITERALS
A string literal is a sequence of characters, which must be quoted in
quotation marks and which must occur on a single line.
All string literals are objects of class String.
Escape sequences as well as Unicode values can appear in string
literals:
1. “Here comes a tab. \t And here comes another one \u0009!”
2. “What’s on the menu?”
3. “\ “String literals are double quoted. \””
4. “Left! \n Right!”
In (1), the tab character is specified using the escape sequence and the
Unicode value respectively.
In (2), the single apostrophe need not be escaped in strings.
In (3), the double apostrophes in the string must be escaped.
In (4), we use escape sequences \n to insert a new line.
BASIC LANGUAGE ELEMENTS:
LITERALS: WHITE SPACES
A white space is a sequence of spaces, tabs, form feeds, and line
terminator characters in a Java source file.
Line terminators can be new line, carriage return, or carriage return-new
line sequence.
A Java program is a free format sequence of characters that is broken into
streams of tokens for further analysis by the compiler.
Separators and operators help us distinguish tokens, but sometimes white
space has to be inserted explicitly as separators.
For example, the identifier classRoom will be interpreted as a single
token, unless white space is inserted to distinguish the keyword class
from the identifier Room.
White space helps not only in separating tokens but also in formatting the
program so that it becomes easy to read.
The compiler ignores the white space once the tokens are identified.
BASIC LANGUAGE ELEMENTS:
LITERALS: COMMENTS
A program can be documented by inserting comments at
relevant places.
These comments are for documentation purpose and are
ignored by the compiler.
Java provides three types of comments:
A single line comment: //... To the end of the line
A multiple-line comment: /*... */
A documentation (Java doc) comment: /**... */
BASIC LANGUAGE ELEMENTS:
LITERALS: COMMENTS
Single Line Comments:
All characters after the comment-start sequence through to the
end of the line constitute a single line comment.
// This comment ends at the end of this line.int age;
Multiple Line Comments:
As the name suggests, it can span several lines.
Starts with /* and ends with */
/* A comment
On several
Lines */
The comment start sequences (//, /*, /**) are not treated
differently from other characters when occurring within
comments, and are thus ignored.
BASIC LANGUAGE ELEMENTS:
LITERALS: COMMENTS
This means trying to nest multiple line comments will
result in compile time error.
For example:
/* Formula for Alchemy.
gold= wizard.makeGold(stone);
/* But it only works on Sundays. */
*/
The second occurrence of comment start sequence /* is
ignored.
The last occurrence of the sequence */ is now unmatched,
resulting in syntax error.
BASIC LANGUAGE ELEMENTS:
LITERALS: COMMENTS
Documentation Comment:
It is a special purpose comment that when placed before
class or class member declarations, can be extracted and
used by the javadoc tool to generate HTML documentation
for the program.
These comments are usually placed in front of classes,
interfaces, methods, and field definitions.
Groups of special tags can be used inside a documentation
comment to provide more specific information.
Such a comment starts with /** and ends with */.
BASIC LANGUAGE ELEMENTS:
LITERALS: COMMENTS
For example:
/**
* This class implements GenericPlugin
* @author Tom
* @Version 1.0
*/