Scanner class Methods in Java

Java- Identifiers & Variables

Identifiers

In java,  identifier is a symbolic name that a programmer gives to various programming elements such as variables, constant, methods and classes etc. So basically, we use identifiers to name the program elements so that we can identify them later.

For example, a class has a name as MCA, it contains some objects as students.The MCA and students are identifiers which represents something.

Naming Conventions

In Java, we have some rules and convention to name Identifiers. If you don't follow these rules,, your program will become erroneous. Lets have a look at these rules,

  1. All the identifiers should start with alphabets, underscore(_) and dollar($) sign can be used, but it is discouraged to use them.
  2. Identifiers must not start with any numeric value. Numeric value can be used in the middle or end but not at beginning.
  3. No spaces and special characters are allowed in identifiers.
  4. An identifier can be several characters long.
  5. An identifier cannot be a Java Keyword.
  6. Identifiers are case sensitive, count and Count are different identifiers.
  7. A good programmer practice to use a identifier that can represent the purpose of the element being used.

Variable

A variable is a name given to a location in the computer's memory so that it can store a value. The value of variable may change during the execution of the program but at a given instance only one value can be stored in it. Variable have changeable values therefore, named as so. Each variable has name and datatype associated with it.
Lets suppose, there is a class of students you want  to make a program in which you will print the value of each students'name and age. Now, think about it what you can do? If you have 5 students,Will you use five names and 5 integers for age? It will create a mess. What if there are 100 students. You will not be able to make program, it's a huge mess.  
Alternate and efficient solution is use a variable "student_name" and for age "student_age".During the program you can modify the value of these 2 variables.You can store each students's data in these variable and print,the procedure goes on until the problem is solved.
That's why variables are important. Use of variable's saves memory.They help in calculations.

Creating variables in Java

To create a variable, use following syntax rules.And you should also have knowledge of datatypes.We'll be learning datatypes in next post.

Syntax:-  datatype variable_name;         OR   datatype variable_name=value;

In the first syntax, we are just declaring a variable. You can assign value to it anytime but before using it.In the second one, we are declaring and defining the variable simultaneously Or initializing it with some value.
Example:- Lets create a variable for subtracting two numbers, for this purpose we need two variable to store values of numbers and third variable to store the result.To create a variable of integer type we will use "int" datatype which is short form of integer.

int a=5;//number 1
int b=2  ;//number 2
int c=a-b; //c  var to store result

Variables follows the same naming conventions as Identifiers.Try creating more variables and simple logic programs like multiplication and division.Happy Coding!.

Comments