As human
language has some specific literals, words, constructors, grammar computer
languages also have these defined in it. Compiler (which is different for every
language and we cannot execute programs in any language without having specific
compiler for it) is responsible for checking whether you are using correct
language constructs, grammar, literals etc in that specific language. If you
want to know the another language which is unknown to you then you are required
to learn the literals of that language, constructs of that language and grammar
to construct your sentences along with the words used in that language;
likewise to learn any computer language you are required to learn all above
particular things and rules to define that language.
Like these
other languages Java has some defined set of characters, literal and constant
words which are listed below:
1. Character Set: Character
set in Java defines the set of characters that can be use in Java programming
language. The allowed set of character that can be used in Java is:
a-z
A-Z
0-9
Unicode characters
2. Identifiers: Identifiers
are the names given to some specific entities of programming language so that
these entities can be identified and processed uniquely by the compiler. Identifiers
are classified in two types as:
a) Built-in Identifiers: These are
reserved keywords in programming language whose meaning cannot be changed by
the programmers. Some built identifiers in Java are:
i.
Data Types:
Data types
|
Required
Bytes
|
Default Value
|
Min Value
|
Max Value
|
Boolean
|
Not defined
|
False
|
True OR
|
False
|
Byte
|
1
|
0
|
-128
|
127
|
Short
|
2
|
0
|
-32768
|
32767
|
Char
|
2
|
0
|
\u0000 0
|
65535
|
Int
|
4
|
0
|
-2^31
|
2^31
|
Long
|
8
|
0
|
-2^63
|
2^63-1
|
Float
|
4
|
0.0
|
1.4E-45
|
3.4028235E38
|
Double
|
8
|
0.0
|
4.9E-324
|
1.7976931348623157E308
|
b) User Defined
identifiers: Identifiers
which are defined by the user to identify the programming entities are called
as user defined identifiers. These identifiers helps programmer to identify the
programming entities uniquely.
Rules to define User defined identifiers in Java are:
i.
Only letters (a-z), (A-Z), digits (0-9) and underscore (_)
can be used to name the identifier.
ii.
There is no restriction on the length of name of identifier.
iii.
Keywords and built in identifiers can not be used as name for
used defined identifiers.
iv.
The name of the identifier should not start with digit (0-9).
v.
Methods to verify the whether the character can be part of
user defined identifier or not are:
boolean isJavaIdentifierPart(char)
boolean isJavaIdenitiferStart(char)
Some
General Recommendations while defining the name of the identifiers in Java:
Ø The name of
identifiers for a type like class, package, interface, enum and annotations
first letter of each word should be written in capital or upper case.
Ø For method
variables first character must be in lower case and first letter of each word
should be in upper case (in case of multiple words) All the other letters
should be in lower case.
Ø If you are
providing any identifier for constant then all letters must be in uppercase. If
multiple words are present then underscore (_) character must be used as
separator.
Ex: final flaot PI;
final float SIMPLE_INTEREST;
3. Control Statements: if,else,switch,case,default,break,continue,return,goto,while,do,for
are some of the general control statements which can be used in Java.
try,
catch, finally, throws, assert are the exception handling statements in Java.
ü Constant
and goto can not be used in source file as the meaning of these keywords is not
defined in Java.
ü True, false
and null are not keywords and they can be used as values for types.
ü Every
keyword in Java is written in lowercase always. Java is case sensitive
programming language.
4. Data Types: Data types
are used indicate the type of data or value that is used. Data types are also
used to declare the variable to indicate the type of data that can be hold by
the variables in future.
i.
Primitive Data types: The
primitive data types are used to declare the variables that can hold actual
data. There are 8 primitive data types as specified by the Sun Microsystems as:
Boolean, byte, short, char, int, long, float, double.
ii.
Reference Type: Reference
types are used to declare user defined types or arrays. In java user defined
types are class, interface, enum, annotations etc. The value for this type will
be reference and it is calculated by the JVM, by performing some operations on
the actual address. In java actual addresses can not be accessed. For reference
type default value is null, byte used is 8 byte.
5. Variables: Variables are used to
indicate the memory locations to access the value present in those particular
locations. Variable can hold some value and it indicates to the address of
memory where actual data is stored.
i.
Instance Variables: A variable
which is declared inside the class and outside any member, constructor,
initialization block without using static keyword are called as instance
variable.
ii.
Static variables: A variable
which is declared inside the class and outside any member, constructor,
initialization block using static keyword are called as instance variable.
iii.
Local variables: A variable
which is declared inside any member, constructor, and initialization block is
called as local variable.
Syntax to declare a variable:
[Modifier(s)]
Syntax to declare and initialize a variable:
[Modifier(s)]=
Syntax to declare and initialize multiple variables:
[Modifier(s)]=,=,=,...,=
6. Constants: Constants are
always declared using the keyword ‘final’. Final variable i.e. constants can
not be reassigned. Once constants are initialized before they are used.
Constants will not be initialized by JVM the programmer is responsible for
initializing the constants. Constants also do not have any default value.