Wednesday, December 28, 2016

Identifiers in oracle

 Identifiers

--------------------------------------------------------------------------------------------------------
Identifier is a name provided to the programming elements like variables, methods, classes, interfaces.....

To provide identifiers in java program, java has provided the following rules & regulations.

A. Identifiers must not be started with a number, Identifiers may start with an alphabet, _ symbol, $          symbol but subsequent symbols may be a number, an alphabet, _ symbol, $ symbol.

EX:  String 9eid = "err";----------------> invalid
String eid = "err-1";---------------> valid
String emp9Id = "err891";---------------> valid
float $esal =   6932145f;---------------> valid
String _eaddr = "Hyd";---------------> valid
String emp_Name = "bali";---------------> valid
--------------

B. Identifiers are not allowing all operators & all special symbol except _ and $ symbol.
EX: 
 String emp.Name = "bali" ----------> invalid
String emp-Name = "bali" ----------> invalid
String emp_Name = "bali" ----------> valid
String emp$Name = "bali" ----------> valid
String emp+Name = "bali" ----------> invalid
String emp@Name = "bali" ----------> invalid
String _$_emp_$_$_Name = "bali" ----------> valid
---------------------------

C. Identifiers are not allowing spaces in the middle.
EX: 
  forName(--); ---------> Valid
for Name(--); ---------> invalid
getInputStream(-); ----> valid
get Input Stream (-); ------> Invalid
---------------------

D. Identifiers must not be duplicated within the same scope, identifiers may be duplicated in two             different scopes.
EX:
class A
{
int i = 10;
short i = 20;-------->error
boolean f = true;
void m1()
{
float f = 23.4f -----> no error
double f = 2850; -----> error
long i = 20; ---------> no error
System.out.println(i); ------> print local variable
System.out.println(this.i); -----> print class level variable
}
}

Note: Do you thinking why long i not giving any error, because it is already defined in class level variables?
And: for this you need to understand variables and memory storage, there are 3 type of memories in JAVA
1. Method Area   -----> it save static variable
2. Stack memory -----> saves local variables
3. Heap memory ------> saves non-static variables in form of objects.

so here in the given examples, int i and short i are state level non-static variable so they are saved in Heap memory while long i is saved in Stack memory, so when JVM search for long i, it is available in Stack memory and no other i variable available in stack memory so it causes no error while int i and short i, both are in same memory, so it causes error.
---------------------------------------------

E. In java application, we are able to use all predefined class names & interface names as identifiers.

EX: 1
int Exception= 10;
System.out.println(Exception);

Status: No compilation error
Output: 10

Note: Here Exception is an int variable.
---------------

EX: 2
String String = "String";
System.out.println(String);

Status: No compilation Error.
Output: String

Note: Here First String is as String class, 2nd String as a variable, 3rd String as value of variable.
--------------

EX: 3
int System = 10;
System.out.println(System);

Status: Compilation Error.
Reason: In java application , if we declare "System" as an integer variable then in the remaining program, we must use that System as integer variable only, we must not use that System as original class name, if we use "System" as original class name then compiler will rise an error.

In the above context if wi want to use System as class name, then we have to use its "fully qualified name" providing class name along their package name is called as "Fully qualified name".

EX:
class Test
{
public static void main(String[] args)
{
int System = 10;
java.lang.System.out.println(System);
System=System + 10;
java.lang.System.out.println(System);
}
}

Status: No compilation error
Output:
10
20
---------------------------

Along with the above rules & regulations, JAVA has provided the following suggestiong to use identifiers in JAVA application:

1. It is suggestible to provide identifier with a particular meaning.
EX:
String aa = "abc123"; ---------> Not Suggestible
String accNo = "abc123"; ------> Suggestible

2. In Java Application, there is no length restriction for the identifiers, but, it is suggestible to manage length of the identifier around 10 symbols.
EX: 
String temporaryemployeeaddress = "Hyd"; -------> Not Suggestible
String tempEmpAddr = "Hyd"; --------------------> Suggestible

3. If we have multiple words with in a single identifier then it is suggestible to separate multiple words with special notation like '-' symbol.
EX:
String tempEmpAddr = "Hyd"; -----> Not Suggestible
String temp_Emp_Addr = "Hyd"; ---> Suggestible


No comments:

Post a Comment