Q1. Can we use a keyword as an identifier ? Explain in detail.
Keywords are the reserved words and there is a predefined meaning of these
keywords known to the compiler. Keywords are used to provide some specific
features and its job is already explained to the compiler. If we use a keyword
as an identifier, we try to assign a new meaning to the keyword which is not
acceptable. Hence, we cannot use a keyword as an identifier.
Q2. Specify the conventions followed in java to name different types of identifiers.
The following conventions are generally followed to name different types of identifiers
A variable name, method name usually start with a lowercase letter
e.g. age, average, etc.
If multiple words are used to name a variable, the words are usually
connected with underscore or follow camel case notation (i.e. capitalize first
letter of each word) e.g. current_speed, currentSpeed, etc.
A class name, interface name usually start with an upper case letter e.g.
Student, Vehicle, etc.
Q3. Find the invalid identifier names from the list below and explain
the reason why these identifiers are invalid.
age: valid
myage: valid
my_age: valid
my-age: invalid, hyphen is not allowed
age@2019: invalid, @ symbol is not allowed
age_2019: valid
2019_age: invalid, cannot start with a digit
myPhone: valid
my Phone: invalid, no space is allowed in an identifier