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:

  1. Begin with an alphabet character
  2. White space is not permitted
  3. Special characters are not allowed except the $ and the _
  4. Java keyword is not permitted

There are naming conventions that most Java programmers follow.

  1. Class names should be nouns that start with an uppercase letter, mixed with the first letter of each internal word capitalized. (ex, Account, BankAccount)
  2. 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())
  3. 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)
  4. 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.