Showing posts with label java tokens. Show all posts
Showing posts with label java tokens. Show all posts

Sunday, 21 December 2014

Java tokens, variables, constants

Java tokens are the atomic elements of Java. Java program are a collection of Java tokens separated by white spaces and comments.

Java is a free form language so it is not must to follow any special indentation rule. The only rule, which needs to be considered, is that the java tokens must be separated by an operator or by a separator or  by at least one white space character. In java, white space is a space, tab, or new line.

More than one white space characters may also appear wherever one white space character is allowed.

Java tokens can be classified into following categories:

  • Keywords
  • Identifiers
  • Literals
  • Operators
  • Separators
Keywords


There are appox. 50 reserved keywords in java. These keywords can not be used as identifiers. All keywords are in lowercase like C/C++.

The keywords const and goto are reserved but are not being used as of now. They  are reserved for the future use.

A new keyword assert was added by jdk 1.4.

In addition to the keywords, Java reserves the following:

true, false and null

These are the literal/constant values defined by Java, you may not use these keywords as identifiers.


Identifiers

An identifier is a word used in a program to name a local variable, method, class, interface, data members, package name etc. Java identifiers are case sensitive.

Name of the identifiers must follow certain rules:


  • A Java identifier must begin with a letter, dollar sign or underscore.
  • The subsequent characters may be digits also.
  • There is no length restriction on the identifiers i.e. they can be of any length.
  • Java allows Unicode characters in identifiers.
It is suggested that following naming convention may be followed while naming java identifiers:


  • Name of all the classes and interfaces start with a leading uppercase letter and each subsequent word also starts with a leading uppercase letter. Rest of the letters must be in lower case. Example: Student, Hello, HelloWorld, EmployeeInfo etc.
  • Names of all the public data members and methods start with a leading lowercase character. When more than one words are used in a name, the second and subsequent words start with a leading uppercase letter. Example: annualSalary, grossSalary, calculateSalary etc.
  • Names of variables that represent constant values use all uppercase letters and underscores between words.


Literals/Constants

A java literal represents a constant value in java. A literal or constant is a value specified in the source as opposed to one determined at run time. Literals may appear on the right side  in an assignment, or may be passed as arguments in method calls. Literals represent constant values so you can not assign a value to any literal i.e. they can not appear on the left side in an assignment.

Literals represent integer (byte, short, int, long) constants, floating point (float, double), constants, Character (char) constants and Boolean (boolean) constants in Java. Java also supports a literal null, which represents a null reference.

Examples:

Integer Literals: 200, -600, 0x1A, 015, 564L

Floating point Literals: 6.89, -3.15, 2.45f, 3.45d, 1.34E-01, 4.56E11

Character Literals: 'x', 'X', '\101', '\n'

Boolean Literals: true, false

String: "Hello", "abc", "xyz"