PROGRAMMING IN JAVA
UNIT 1- DECLARATIONS AND ACCESS CONTROL
JAVA IDENTIFIERS & KEYWORDS
Java Identifiers
- · An Identifiers is a name given to program elements such as class, variable, field, method, or constructor.
- · In Java programming language an identifier is a sequence of alphabets or digits.
- ·
The two aspects of Java identifiers are
o Legal
identifiers: The rules the compiler uses to determine whether a name is
legal.
o Oracle's Java
Code Conventions: Oracle's
recommendations for naming classes, variables, and methods.
Legal
Identifiers
- Technically, legal identifiers must be composed of only
Unicode characters, numbers, currency symbols, and connecting characters (such
as underscores).
- The rules for Legal Identifiers:
o
Identifiers must start with a letter, a currency
character ($), or a
connecting character such as the underscore (_).
o
Identifiers cannot start with a digit!
o After the first
character, identifiers can contain any combination of letters, currency
characters, connecting characters, or numbers.
o There is no
limit to the number of characters an identifier can contain.
o You can't use a
Java keyword as an identifier.
o Identifiers in
Java are case-sensitive; For Example , foo and FOO are two different
identifiers.
Examples of legal identifiers
follow:
o int _a;
o int $c;
o int ______2_w;
o int _$;
o int
this_is_a_very_detailed_name_for_an_identifier;
The following are illegal identifiers:
o int :b;
o int -d;
o int e#;
o int .f;
o int 7g;
Oracle's Java Code Conventions
- Oracle has created a set of
coding standards for Java and published those standards in a document titled as
"Java Code Conventions", which is available at java.oracle.com.
- The naming standards that Oracle recommends are,
1.
Classes and interfaces :
o
The first letter should be capitalized, and if several words are linked
together to form the name, the first letter of the inner words should be uppercase (a
format that's sometimes called "CamelCase").
o
For classes, the names should typically be nouns. Here are some examples:
§ Dog
§ Account
§ PrintWriter
o
For interfaces, the names should typically be adjectives, like these:
§ Runnable
§ Serializable
2.
Methods:
o
The first letter should be lowercase, and then normal CamelCase rules
should be used. In addition, the names should typically be verb-noun pairs.
o
For example:
§ getBalance
§ doCalculation
§ setCustomerName
3.
Variables:
o
Like methods, the CamelCase format should be used, but starting with a
lowercase letter. Oracle recommends short, meaningful names, which sounds good
to us.
o
Some examples:
§ buttonWidth
§ accountBalance
§ myString
4.
Constants:
o
Java constants are created by
marking variables static and final.
o
They should be named using uppercase letters with underscore characters as
separators:
§ MIN_HEIGHT
Java Keywords
·
Keywords are predefined,
reserved words used in Java programming that have special meanings to the
compiler.
·
For example:
int rollno;
·
Here, int is a keyword. It
indicates that the variable “rollno” is of integer type.
·
Keywords cannot be used as
variable name (or identifiers) as they are part of the Java programming
language syntax.
·
The complete list of all
keywords in Java programming.
Java Keywords List |
||||
abstract |
assert |
boolean |
break |
byte |
case |
catch |
char |
class |
const |
continue |
default |
do |
double |
else |
enum |
extends |
final |
finally |
float |
for |
goto |
if |
implements |
import |
instanceof |
int |
interface |
long |
native |
new |
package |
private |
protected |
public |
return |
short |
static |
strictfp |
super |
switch |
synchronized |
this |
throw |
throws |
transient |
try |
void |
volatile |
while |