Lexical Issues in Java
Overview
• Java programs consist of several atomic
elements that include whitespace, identifiers,
literals, comments, operators, separators, and
keywords. These elements collectively define
the syntax and structure of Java programs.
Whitespace
• Java is a free-form language, which means:
• - No strict rules for indentation.
• - Programs can be written on a single line or
spread across multiple lines.
• - Whitespace characters include: Space, Tab,
Newline.
• - Whitespace separates tokens not already
delineated by operators or separators.
Identifiers
• Identifiers are names used for classes,
methods, and variables.
• Rules for Identifiers:
• - Can contain letters, digits, underscores, and
dollar signs.
• - Must not begin with a digit.
• - Java is case-sensitive.
• Examples of Valid Identifiers: AvgTemp, count,
a4, $test, this_is_ok
Literals
• Literals represent constant values directly in
the code.
• Examples:
• - Integer: 100
• - Floating-point: 98.6
• - Character: 'X'
• - String: "This is a test"
• Usage: Literals can be used wherever values of
their types are required.
Comments
• Java supports three types of comments:
• 1. Single-line comments: Start with //.
• 2. Multi-line comments: Enclosed between /*
and */.
• 3. Documentation comments: Start with /**
and end with */. Used to generate HTML
documentation.
Separators
• Separators are symbols that structure Java
programs.
• Commonly used separators:
• - ( ) Parentheses: Contain parameter lists,
define precedence, and enclose expressions or
control statements.
• - { } Braces: Define blocks of code, classes,
methods, or local scopes.
• - [ ] Brackets: Declare array types and access
array elements.
Keywords
• Java defines 50 keywords that form the core
syntax of the language.
• Key Points about Keywords:
• - Cannot be used as names for variables,
classes, or methods.
• - Reserved keywords: const and goto (not used
but reserved for future use).
• - Reserved literals: true, false, and null.
• Examples of Keywords:
Conclusion
• Understanding these lexical elements is crucial
for writing syntactically correct Java programs.
They provide the building blocks for defining
structure, behavior, and data in Java
applications.