Identifiers


Identifier : A name in java program is called identifier which can be used for identification purpose. It can be method name, variable name, class name or level name.

Ex:
class Test{
public static void main(String [] args){
int x = 10;
}
}


Total identifiers  : Test, main, String (pre-defined java class), args, x --> 5 identifiers

Rules for defining java identifers: 
1) Only allowed symbols are 
a to z, A to Z, 0 to 9, $, _
ex: total_number  -->  valid
total# --> invalid

if we use any other character we will get compile time error.

2) Identifier should not start with digit.
ex:  123total --> invalid
total123 --> valid

3) Java identifiers are case-sensitive.  Java language itself is case sensitive programming language.
ex: int number;
int Number;
int NUMBER;
* all are valid

4) There is no length limit for identifier but not recommended to take too lengthy identifier.

5) We can't use reserved words as identifiers.
ex: int x =10; --> valid
int if = 20; --> invalid

6) int String = 888;
System.out.println(String);  // 888   valid
All java predefined classname and interface name. We can use as identifiers.
Even though it is vlid but it is not a good programming practice because it reduces readability and creates confusion.



Reserved Words

In java some words are reserved to represent some meaning or functionality . Such type of words are called reserved words.
There are total 53 reserved words in java.

Reserved words(53)
-- Keywords (50)
-- used keywords(48) -- if else...
-- unused keywords(2) -- goto const
-- Reserved Literals(3)  -- true, false, null


Keywords for Data Types
---------------------------------
byte
short
int
long
float
double
boolean
char


Keywords for flow control
----------------------------------
if
else
switch
case
default
do
while
for
break
continue
return


Keywords for modifiers
-------------------------------

public
private
protected
static
final
abstract
synchronized
native
strictfp  --1.2v
transient
volatile


Keyword for Exception Handling
-------------------------------------------
try
catch
finally
throw
throws
assert (1.4 v)

Class related keywords
------------------------------
Class
Interface
extends
implements
package
import

Object related keyword
-----------------------------
new
instanceof
super
this

return type keyword
---------------------------
void

In java return type is mandatory. If a metho won't return anything then we have to declare that method with void return type.
But in C language return type is optional and default return type is int.


--------Unused Keyword--------
There are just 2 unused keyword
goto --> usage of goto created several problems in old languages, Hence sun people banned this keyword in java.
const --> use of final instead of const.

By mistake if you use these keywords , you will get compile time error.



-------Reserved Literals-------
true    --> value for boolean data type.
false --> value for boolean data type.
null --> default value for object reference.


Enum keyword (1.5 v)
----------------------------
We can use enum to define a group of named constants.
ex:
enum month{
Jan,Feb,...,Dec;
}


Conclusions
----------------
# All 53 reserved words in java contains only lowercase symbol
# In java we have only new keyword and there is no delete keyword because destruction of useless objects is responsibility of garbage collector.
# The following are new keywords in java
--> strictfp (1.2 v)
--> assert (1.4 v)
--> enum (1.5 v)
# --> strictfp but not strictFp
  --> instanceof but not instanceOf
  --> synchronized but not synchronize
  --> const but not constant
  --> implements but not implement
  --> import but not imports
  --> extends but not extend



Interview Question 
Que-  Which of the following list contains java reserved words?
new, delete
goto, constant
break, continue, return, exit
final, finally, finalize
throw, throws, thrown
notify, notifyall
implements, extends, imports
sizeof, instanceof
instanceOf, strictFp
byte, short, Int
none of the above


Comments

Popular posts from this blog

Floating Point Literals

Array Initialization in Java

Data Types