Variables In Java with examples

Variables In Java with examples

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 Main {
  public static void main(String[] args) {
    int rollno = 10;
    String name = "TutorialWorld";
    float marks = 43.2f;
    System.out.println("Name is " + name);
    System.out.println("Roll number is " + rollno);
    System.out.println("Marks is " + marks);
  }
}

Output

Name is TutorialWorld
Roll number is 10
Marks is 43.2

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  : 10
  2. String Literal : “TutorialWorld”
  3. Float Literal : 43.2

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 identifiers