CoreJavaTutorials
Contribute Us | Ask Question |
z

Variables in Java

A variable in java like other programming language are used to store or hold the values that are used in a program.
Before using the variable we have to declare the variable and then assign value to it.
We can also assign data to a variable with its declaration as well as within a method. The simple example to show the use of variable is below:

public class VariableExample
{
   public static void main(String[] args)
   {
      // Declare variables to hold data and assign data.
      int    rollno=1;
      String name="shashank";
      float marks=97.5;

      // Display the data in variables
      
      System.out.println("Your name is " + name);
      System.out.println("Your roll number is " + rollno);
      System.out.println("Your marks is " + marks);
   }
}
		

Output

compile command javac VariableExample.java run command java VariableExample

Output: shashank 1 97.5

Literals in Java

A constant value in Java is said as a literal representation of it. Literals are represented directly in the code without any computation.
So a constant value assign to a variable without any computation or calculation is know as literals in java. for example:
int a=10;
In above program, we have used following types of literals:

1 - Integer Literal "shashank" - String Literal 97.5 - Float Literal

Identifiers in Java

Identifiers can be referred as anything that is used to uniquely identified the variables, methods, classes, packages and interfaces in a program.
Identifiers must have to follow the rule of java for identifiers that are given below:

1) A Java identifier must begin with a letter, underscore, or the dollar sign.
2) A Java identifier consists of letters, digits, the underscore character (_), and the dollar sign ($) ; no other symbols are permitted to form an identifier.
3) Java is case sensitive means uppercase and lowercase letters are considered different.
4) Identifiers can be any length means unlimited .But it is a good code practice to have short and suitable identifers