Keywords, Identifiers, Comments
Keywords
In a computer language, keywords are reserved words for particular uses.
Identifiers
The names of classes, variables, and methods are collectively called identifiers.
Java identifiers must adhere to the following rules:
- Begin with an alphabet character
- White space is not permitted
- Special characters are not allowed except the $ and the _
- Java keyword is not permitted
There are naming conventions that most Java programmers follow.
- Class names should be nouns that start with an uppercase letter, mixed with the first letter of each internal word capitalized. (ex, Account, BankAccount)
- Method names should be verbs that start with a lowercase letter, mixed with capitalized by the first letter of each internal word. (ex, deposit(), getAccountNo())
- Variable names should be nouns that start with a lowercase letter, mixed with capitalized by the first letter of each internal word. (ex, balance, accountNo, bankAccountNo)
- Constant names should be all uppercase with words separated by underscore _. (ex, SWIFT_CODE)
We haven't covered Java constants yet. How to create constants in Java will be explained later.
Bank accounts are not objects in the real world because they are invisible. But in the software world, it can be objects.
Follow the naming conventions and create a bank account class.
class Account { String accountNo; int balance; String getAccountNo() { return accountNo; } void setAccountNo(String account) { accountNo = account; } int getBalance() { return balance; } void deposit(int amount) { balance = balance + amount; } void withdraw(int amount) { balance = balance - amount; } }
Comments
Keywords and identifiers are essential elements of the code. Although not required, comments are also a component of the code. Comments are text added to make the source code easier to understand by programmers. Compilers ignore comments.
The Java language supports three kinds of comments.
//text
The compiler ignores everything from // to the end of the line.
/* text */
The compiler ignores everything between /* and */.
/** text */
The compiler ignores everything between /** and */.
You can generate Java documentation using the JDK's Javadoc with these comments.