Java Identifiers, Reserved Keywords And Control Statements
Identifiers in Java are one of the basic fundamentals of Java that is mandatory for any Java learner. Without learning the identifiers, its rules and naming convention, you can’t efficiently program in Java. The naming conventions are optional, but you should follow them as a rule so that it increases the readability of the code.
In simple it is a variable name in java. Let’s say you allocated a memory for an integer i.e. int a; so now how you will identify the memory that you have allocated, by using the identifier name i.e. a. If there were no identifiers in programming then you have to write address of the memory that you have allocated. So they just made it simple to keep allocated memory in track in simple human language. Keywords also are known as reserved words are the pre-defined identifiers reserved by Java for a specific purpose that inform the compiler about what the program should do. A Keyword is that which have a special meaning those already explained to the java language like int, float, class, public, etc. these are the reserved keywords. These special words cannot be used as class names, variables, or method names, because they have special meaning within the language. Control Statements in Java is one of the fundamentals required for Java Programming. It allows the smooth flow of a program.
And of course there are some rules to define an identifier in java so let’s see
1. Identifier must not start with any numeric or it is always recommendable to starts some of the identifiers with small case.
2. It must not contain any special symbol other than (_ or $).
3. Identifier must not contain any blank space in between.
4. Identifiers may contain numeric but it should not be in starting.
5. Identifiers in Java are case-sensitive means oop and OOP are two different identifiers.
6. Keywords cannot be used as an identifiers. For example “int while = 156;” is an invalid statement as while is a reserved word.
Valid and Invalid Identifiers
Valid Identifiers
Invalid Identifiers
Reserved Keywords
Keywords are predefined words by Java it conveys special meaning to the Java compiler. It cannot be used as a variable or object, class name or methods name. Reserved words which are having predefined meaning in Java. All the keywords are defined in lower case and the meaning of these keywords can’t be modified.Let us start our discussion with what is keywords in Java and then we will discuss some main keywords in Java that we frequently use in our Java programs. In Java there are 51 keywords, but 2 of them are not used which are-goto and const. Only 49 keywords are used in Java. All of them have different purposes and meanings.
Below is the Java Keyword List:
Just see in a brief to get
1 — Datatypes/return types :-
byte,short,int,long,float,double,char,Boolean,void
2 — Access modifiers/security Related :-
private,public,protected,static,final,abstract,native,voatie,transient,synchronized,strictfp
3 — Flow controlling Related keywords :-
If,else,for,while,do,case,default,break,return,continue,switch,assert
4 — Class Related keywords :-
Class,extends,import,implements,interface,package,enum
5 — Object Related keywords :-
this,super,new,instanceof
6 — Exception handling related keywords :-
try,catch,throw,throws,finally
Reserved words is also predefined word but it does not have any valid functionality and its functionality is not register with jvm.
Ex. :- const,goto (used in C++)but not used in java.
In java instead of “const” word we can use final keyword.
In java instead of “goto” word we can use break,continue,return keyword.
Control Statements
Control flow statements alter the flow of execution and provide better control to the programmer on the flow execution. Whenever we need to execute a set of statements based on some condition then we need to use control statements. Control statement executes the set of statements based on the condition is true or false. If condition is true then Control flow statement executes the set of instruction inside it otherwise if the condition is false then Control goes to the next Control flow or next defined statements. If a program has no control statements, the JVM executes it, statement by statement, in sequential order.
In java control statements are categorized into 3 groups:
1. Selection or Decision Making Statements:
Allow the program to choose different parts of the execution based on the outcome of an expression.
2. Iteration Statements:
It enables program execution to repeat one or more statements.
3. Jump Statements:
It enables your program to execute in a non-linear fashion
Let’s see one bye one in detail
Selection or Decision Making Statements:
1. if statements:
The Statements only gets executed when the Condition given inside the If Statement is True otherwise the program simply skip the statements inside the If Statements and execute the next given command. The syntax and execution flow of if statement is as follows.
2. if-Else Statements:
The if-else statement checks the given condition then decides which block of statements to be executed based on the condition result. If the condition is True, then the true block of statements is executed and if it is False, then the false block of statements is executed. The syntax and execution flow of if-else statement is as follows.
3.switch statements:
Using the switch statement, one can select only one option from more number of options very easily. In the switch statement, we provide a value that is to be compared with a value associated with each option. Whenever the given value matches the value associated with an option, the execution starts from that option. In the switch statement, every option is defined as a case.
The switch statement has the following syntax and execution flow diagram.
Iteration Statements:
- while statements:
The while statement is used to execute a single statement or block of statements repeatedly as long as the given condition is TRUE.
2. do-while statements
The do-while statement is used to execute a single statement or block of statements repeatedly as long as given the condition is TRUE. The do-while statement has the following syntax.
3.For Statements
The for statement is used to execute a single statement or a block of statements repeatedly as long as the given condition is TRUE. The for statement has the following syntax and execution flow diagram.
Jump Statements
1.break statement
The break statement in java is used to terminate a switch or looping statement. That means the break statement is used to come out of a switch statement and a looping statement like while, do-while, for, and for-each.
2.continue statement
The continue statement is used to move the execution control to the beginning of the looping statement. When the continue statement is encountered in a looping statement, the execution control skips the rest of the statements in the looping block and directly jumps to the beginning of the loop. The continue statement can be used with looping statements like while, do-while, for
conclusion:
In this article, we learnt about identifiers which are a core concept in Java. We learnt about them, what are the different rules to name them and much more. Then we have discussed various keywords used in Java. Java supports a total of 51 keywords out of which 49 keywords are currently used and 2 are not currently used. After that we have discussed about the control statements and their types and how they are used to control the flow of execution in your code. It must be used efficiently to make the program effective and user-friendly.
❤ ❤ Thanks for reading this post❤❤
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!
Clap 👏 If this article helps you.