Read the first chapter
The whole of chapter one, free. About 5 min. Turn the pages with the arrows, your keyboard, or a swipe.
Chapter 1
Java Lexical Rules and Keywords
Key Concepts
This chapter covers Java’s lexical rules: the basic tokens and symbols that form valid source code. These rules matter in exams because errors in names, literals, comments, separators, and keywords can prevent compilation.
• Whitespace separates tokens; spaces, tabs, and line breaks are usually ignored.
• Identifiers name classes, methods, variables, and packages.
• May contain letters, digits, _, and $.
• Cannot begin with a digit.
• Cannot be a Java keyword.
• Java is case-sensitive: total and Total differ.
• Literals are fixed values written directly in code, such as 25, 3.14, 'A', "Java", and true.
• Comments document code and are ignored by the compiler:
• // single-line
• /... / multi-line
• /*... / documentation comment
• Separators organize code: (), {}, [],;,,,., and:.
• Keywords are reserved words with predefined meanings, such as class, public, static, void, and return.
Before you continue: Why is 2marks invalid as an identifier, while marks2 is valid?
Key Terms
• Token - The smallest meaningful unit of Java source code.
• Identifier - A programmer-defined name for a program element.
• Literal - A fixed value written directly in source code.
• Comment - Text ignored by the compiler and used for explanation.
• Separator - A symbol that divides or organizes Java code.
• Keyword - A reserved word that cannot be used as an identifier.
• Whitespace - Spaces, tabs, and line breaks used to separate code elements.
Active Recall
• Token
__________________________________________________
__________________________________________________
• Identifier
__________________________________________________
__________________________________________________
• Literal
__________________________________________________
__________________________________________________
• Comment
__________________________________________________
__________________________________________________
• Separator
__________________________________________________
__________________________________________________
• Keyword
__________________________________________________
__________________________________________________
• Whitespace
__________________________________________________
__________________________________________________
Worked Examples
Example 1: Valid and invalid identifiers
• studentName is valid because it starts with a letter.
• marks2 is valid because digits may follow the first character.
• 2marks is invalid because an identifier cannot begin with a digit.
• class is invalid because it is a keyword.
• total_marks is valid; underscore is allowed.
Now you try: Classify value, 7value, static, and value$.
__________________________________________________
__________________________________________________
__________________________________________________
Example 2: Reading literals and separators
int count = 10; char grade = 'A'; String subject = "Java"; System.out.println(subject + ": " + grade); • 10 is an integer literal.
• 'A' is a character literal; character values use single quotes.
• "Java" is a string literal; strings use double quotes.
•; ends each statement.
• () contains method arguments, and. accesses a member.
Now you try: Identify the literals and separators in double rate = 2.5;.
__________________________________________________
__________________________________________________
__________________________________________________
Example 3: Comments and keywords
public class Demo { // Program entry point public static void main(String[] args) { int number = 5; /* integer literal */ System.out.println(number); } } • public, class, static, void, and int are keywords.
• Demo, main, String, args, and number are identifiers.
• The two comments are ignored during compilation.
• Braces define blocks; brackets define an array type.
Now you try: List the keywords and identifiers in private int total = 20;.
__________________________________________________
__________________________________________________
__________________________________________________
Practice Questions
• (Easy) Define whitespace and give two examples.
__________________________________________________
__________________________________________________
__________________________________________________
• (Easy) State whether firstName, 9items, and while are valid identifiers.
__________________________________________________
__________________________________________________
__________________________________________________
• (Medium) Identify the literal types in boolean ready = true; char symbol = '#';.
__________________________________________________
__________________________________________________
__________________________________________________
• (Medium) Explain the difference between // and /... / comments.
__________________________________________________
__________________________________________________
__________________________________________________
• (Hard) Identify the keywords, identifiers, literals, and separators in public static int sum = 25;.
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
Answer Key
• Whitespace includes spaces, tabs, and line breaks; it separates tokens.
• firstName valid; 9items invalid; while invalid because it is a keyword.
• true is a boolean literal; '#' is a character literal.
• // ends at the line break; /... / may span multiple lines.
• Keywords: public, static, int; identifiers: sum; literal: 25; separators: =,;.
Exam Tips & Common Mistakes
• Do not confuse character literals ( 'A' ) with string literals ( "A" ).
• Do not use keywords such as class, int, or return as identifiers.
• Check the first character of every identifier; digits may not appear first.
• Remember that Java
ignores comments, but comments cannot occur inside a token such as an identifier or keyword.
• Use indentation and line breaks to improve readability; they do not normally change program meaning.
• In an exam answer, classify each item precisely: token, identifier, literal, comment, separator, or keyword.
• Check spelling and capitalization because Java keywords and identifiers are case-sensitive.
• Remember that Java keywords are lowercase and cannot be used as names.
Examiner-focused tips:
• When asked to identify lexical elements, quote the exact source text and label each item.
• For code-tracing questions, first remove comments and whitespace mentally, then locate separators and keywords.
• In a definition question, include both the rule and a short Java example.
Quick Reference
Element
Rule
Example
Whitespace
Separates tokens; usually ignored
int n;
Identifier
Name; cannot start with a digit or be a keyword
studentCount
Integer literal
Whole-number value
42
Floating-point literal
Decimal value; f may mark float
3.14, 2.5f
Character literal
One character in single quotes
'A'
String literal
Text in double quotes
"Java"
Boolean literal
true or false
boolean ok = true;
Comment
Documentation ignored by compiler
// note
Separator
Marks structure or boundaries
;, {}, (), [],,,.
Keyword
Reserved Java word
class, if, return
Rules to remember:
• Identifiers: letters, digits, _, and $; the first character cannot be a digit.
• Java is case-sensitive: Count and count are different.
• Statements usually end with;.
• Blocks use { and }.
• Array declarations use [].
• Method calls use (); member access uses..
• Comments: // for one line, /... / for a block, and /*... / for documentation.
Mnemonic: WILCSK - W hitespace, I dentifiers, L iterals, C omments, S eparators, K eywords.
• Whitespace separates source-code tokens.
• Identifiers name program elements; keywords are reserved.
• Literals are fixed values written directly in code.
• Comments are ignored by the compiler.
• Separators show structure and boundaries.
Confidence Check
• I can explain why whitespace is used in Java source code. [ ] 1 [ ] 2 [ ] 3 [ ] 4 [ ] 5
• I can test whether an identifier
End of chapter one. 4 more chapters in the full book.
Swipe or use the arrows to turn the page
What's inside: 5 chapters
- 1. Java Lexical Rules and Keywords
- 2. Primitive Types, Casting, Arrays, Operators
- 3. Control Statements and Jump Flow
- 4. Classes, Methods, and Polymorphism
- 5. Exceptions, Threads, Enums, Generics
About this book
"Java OOP Lab Manual" is a study guide book by chandrashekhar basavaraj with 5 chapters and approximately 4,397 words. Java programming fundamentals, OOP concepts, and lab programs.
This book was created using Inkfluence AI, an AI-powered book generation platform that helps authors write, design, and publish complete books. It was made with the Study Guide Generator.
Frequently Asked Questions
What is "Java OOP Lab Manual" about?
Java programming fundamentals, OOP concepts, and lab programs
How many chapters are in "Java OOP Lab Manual"?
The book contains 5 chapters and approximately 4,397 words. Topics covered include Java Lexical Rules and Keywords, Primitive Types, Casting, Arrays, Operators, Control Statements and Jump Flow, Classes, Methods, and Polymorphism, and more.
Who wrote "Java OOP Lab Manual"?
This book was written by chandrashekhar basavaraj and created using Inkfluence AI, an AI book generation platform that helps authors write, design, and publish books.
How can I create a similar study guide book?
You can create your own study guide book using Inkfluence AI. Describe your idea, choose your style, and the AI writes the full book for you. It's free to start.
Write your own study guide book with AI
Describe your idea and Inkfluence writes the whole thing. Free to start.
Start writingCreated with Inkfluence AI