4 Prog - Fundamentals 1
4 Prog - Fundamentals 1
A name in Java Program is called identifier, which can be used for identification purpose. It can
be method name, class name, variable name, label name.
class Student
{
public static void main(String args[])
{
int x=10;
}
}
Answer: 5
class Student
{
public static void main(String args[])
{
int x=10;
}
}
Student-user defined class name
main- method name
String-predefined class name
args and x are variable name
By mistake if you are using any other character, you will get compile time error.
even_number valid
even# invalid
even2 valid
2even invalid- identifiers cannot start with digits.
class Demo
{
public static void main(String args[])
{
int number =10; //Valid/Invalid
int Number =10; // Valid/Invalid
int NUMBER =10; //Valid/Invalid
//int number=20; //Valid/Invalid
}
}
class Demo
{
public static void main(String args[])
{ int _x=5;
int x_=6;
int $=3;
int 123x=30;
int #=5;
int num=20;
int Num=30;
int NUM=40;
int num=50;
int x=10;
int xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=20;
System.out.println(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx);
}}
Note :
There is no length limit for java identifiers, Even you can take 100000000000000000000
length or any length, but it is not recommended to take too lengthy identifiers.
class ReserveW
{
public static void main(String args[])
{
int for =10;
int if =20;
int class =50;
int cat =70;
}}
class Demo1
{
public static void main(String args[])
{
int String =100;
int Runnable=200;
System.out.println(String);
System.out.println(Runnable);
}
}
output: 100
200
Question : Valid or not????
Note:
All predefined java class names and interface names, we can use as java identifiers.
Even though it is valid but it is not a good programming practice.
It reduces readability and creates confusion.
total_num valid
total# inv
123total inv
total123 valid
ca$h valid
Integer valid
int inv
Int valid
_$_$_$ valid
all@ inv
Upes2020iot valid
Reserved
Word (53)
Reserved
Keyword(50) literals(3)
Used
keywords(48)
Unused
Keyword(2)
Keywords for modifiers: 11 (Actually they are 12, default already included in flow control
category)
20. public
21. private
22. protected
23. static
24. final
Note: goto and const are unused keyword and if you are trying to use we will get compile
time error.
Note: All 53-reserve words in java contains only lower case alphabet symbols.
Note: In java we have only new keyword, and there is no delete keyword because destruction
of useless objects is the responsibility of garbage collector.
enum Month
{
JAN,FEB…..DEC;
}
class Reserveword