0% found this document useful (0 votes)
130 views

4 Prog - Fundamentals 1

Identifiers in Java are used to name classes, methods, variables and other entities. They can contain letters, digits, underscores and dollar signs but cannot start with a digit. There are 53 reserved words in Java that have special meanings and cannot be used as identifiers. Common reserved words include public, private, class, if, else, for, while and void.

Uploaded by

adit.jain606
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

4 Prog - Fundamentals 1

Identifiers in Java are used to name classes, methods, variables and other entities. They can contain letters, digits, underscores and dollar signs but cannot start with a digit. There are 53 reserved words in Java that have special meanings and cannot be used as identifiers. Common reserved words include public, private, class, if, else, for, while and void.

Uploaded by

adit.jain606
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Identifiers in Java

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.

How many identifiers are present in the following program?

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

Object Oriented Programming (CSEG2016)


Rules for Defining Java Identifiers:
Following are the allowed characters for identifiers in java:
A to Z
a to z
0 to 9
$
_ (underscore)

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.

Note: Java identifiers are case sensitive.


Note: Java language itself is treated as case sensitive programming language.

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
}
}

Note: We can differentiate with respect to case.

Object Oriented Programming (CSEG2016)


How many characters are allowed in java for identifiers?
32
64
128
256
512
No limit

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);
}}

Object Oriented Programming (CSEG2016)


class Demo
{
public static void main(String args[])
{
int _x=5;
int x_=6;
int $=3;
int 123x=30; //illegal character
int #=5;//illegal character
int num=20;
int Num=30;
int NUM=40;
//int num=50;//already defined
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.

Object Oriented Programming (CSEG2016)


Can we use reserve word for java identifiers ?

class ReserveW
{
public static void main(String args[])
{
int for =10;
int if =20;
int class =50;
int cat =70;
}}

Note: We cannot use reserve words as identifiers.


Can you tell what is output, we are going to get???

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????

Object Oriented Programming (CSEG2016)


Answer : 100% valid
These are the predefined class or Interface of Java.

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.

Which are following valid/invalid java Identifiers;TODO

total_num valid
total# inv
123total inv
total123 valid
ca$h valid
Integer valid
int inv
Int valid
_$_$_$ valid
all@ inv
Upes2020iot valid

Object Oriented Programming (CSEG2016)


Reserved Word:
In any language-normal language or programming language, some words are reserve word
like English
apple
students
run
sleep 
god 
dog 
odg 
In java some words are reserve words to present some meaning or functionality, such type of words
are called reserve words.
How many reserve word are there in java?
40
45
50
53
55
63
Answer: 53

abstract assert boolean break byte case


catch char class const continue default
double do else enum extends false
final finally float for goto if
implements import instanceof int interface long
native new null package private protected
public return short static strictfp super
switch synchronized this throw throws transient
true try void volatile while

Object Oriented Programming (CSEG2016)


50 (keyword)+ 3(reserve literals) true,false and null

50 keyword= used keyword 48(if else…..)+unused keyword 2(goto,const)

Reserved
Word (53)

Reserved
Keyword(50) literals(3)

Used
keywords(48)

Unused
Keyword(2)

Object Oriented Programming (CSEG2016)


Keywords for data types: 8
1. byte
2. short
3. int
4. long
5. float
6. double
7. boolean
8. char

Keywords for flow control: 11


9. if
10. else
11. switch
12. case
13. default
14. while
15. do
16. break
17. continue
18. return
19. for

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

Object Oriented Programming (CSEG2016)


25. abstract
26. synchronized
27. native
28. strictfp- 1.2 version
29. transient
30. volatile

Keywords for exception handling 6


31. try
32. catch
33. finally
34. throw
35. throws
36. assert 1.4 version

Class related keywords: 6


37. class
38. interface
39. extends
40. implements
41. package
42. import

Object related keywords: 4


43. new
44. instanceof
45. super
46. this
Return type keyword:1
47. void (default return type in java- void, default return type in c language-int)

Object Oriented Programming (CSEG2016)


Group of named const:1
48. enum
Unused keyword:2
49. goto- uses of goto created several problems in old languages, hence SUN people banned
this keyword in java.
50. const- use final instead of const

Note: goto and const are unused keyword and if you are trying to use we will get compile
time error.

Reserved word -literals:3


51. true: value for Boolean data type
52. false : value for Boolean data type
53. null default value of object reference

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 :1 –In which version this one came.?? 1.5 version


when we should go enum: if you want to define group of named constant

enum Month
{
JAN,FEB…..DEC;
}

Object Oriented Programming (CSEG2016)


Which are the following list contains java reserve words:
new, delete
goto , constant
break,continue, return,exit
final,finally,finalize-method
throw, throws,thrown
notify, notifyall
implements, extends, imports
sizeof, instanceof-not in java
istanceif,strictFp
byte,short,Int
none of these-true

class Reserveword

Automatically they are come in blue color.

which of the following are java reserved words:


public
static
void
String
args

Object Oriented Programming (CSEG2016)

You might also like