(d) Find out the valid and invalid identifier in the following list
1st Time, Total_amount, RollNo21. VOLUME, sums, #myriame, rakesh, employee@dav
Answers
Answer:
Valid Identifiers:-
1) Total_amount
2) RollNo21 (If You have Put the dot (.) at the end then IT is Invalid)
3) VOLUME
4) sums
5) rakesh
Invalid Identifiers:-
1) 1st Time ( as it is started from a number )
2) #myriame ( as it contain a invalid Statement # hash in it )
3) employee@dav ( as it contains invalid Statement @ )
Please Make them Correct and Never forget the Rules of Naming Identifiers as Described Below:-
In Java (as in many programming languages), the names of the boxes are called variable names. The word "variable" means changing, because we can change the value inside the box. We can't change its name, however.
Variable names fall in the category of something known as identifiers. Identifiers are names given to things in Java. This includes variables, methods, classes, etc. So far, you don't know anything about methods or classes. We'll get to them in future lessons.
An identifier refers to names of "things" in Java.
Java has strict rules about valid identifiers. Make sure you understand them (they aren't difficult to master) so you don't make errors when you use them.
But First...
But first, let's define a few sets.
alpha This is the set of alphabetic characters { a, b, ..., z, A, B, ... Z }. It contains the 26 uppercase letters and the 26 lowercase letters. We call this set alpha.
digit This is the set of digits { 0, 1, ..., 9 }. It contains the 10 digits from 0 to 9.
We also have underscore, which is the character '_' (the single quotes are used to make it easier to see), and dollar sign, '$'.
Identifiers contain characters from any of: alpha, digit, underscore, and dollar sign. You can't use spaces or tabs or symbols like #, @, !, and so forth in an identifier.
Wait, we're not done yet. More rules are coming.
Syntax of an Identifier
Syntax is a grammatical rule. Here is the syntax for valid Java identifiers:
Each identifier must have at least one character.
The first character must be picked from: alpha, underscore, or dollar sign. The first character can not be a digit.
The rest of the characters (besides the first) can be from: alpha, digit, underscore, or dollar sign. In other words, it can be any valid identifier character.
Put simply, an identifier is one or more characters selected from alpha, digit, underscore, or dollar sign. The only restriction is the first character can't be a digit.
Examples of Valid Identifiers
Here are some valid identifiers:
aaa
sales_tax
_circleArea
box100width
$directory
ab1234$$
Although you have great flexibility for identifiers, we're going to make more restrictions for which identifiers we should use.
But first, to look at some invalid identifiers.
Examples of Invalid Identifiers
It's easy to make a mistake and use a bad identifier.
1ab (ERROR: first character starts with a digit)
num-oranges (ERROR: dash is not permitted in identifiers)
num oranges (ERROR: space is not permitted in identifiers)
Java Style Identifiers
Java has a particular way of writing variable names. This is called Java-style. Style refers to something that is recommended, but not required in Java. In other words, the compiler won't complain if you break style rules. However, other programmers may think you're using "bad" style.