1.4 Variables and Literals

// This program uses variables and literals

public class Student
{
	public static void main(String[] args)
	{
		// variable declaration
		int rollno;
		String name;
		Double marks;

		// assignment 
		rollno = 19;
		name = "David"
		marks = 89.8;

		// display 
		System.out.println("Your roll number is " + rollno);
		System.out.println("Your name is " + name);
		System.out.println("Your marks is " + marks);
	}
}

When you compile and execute this program, the following three lines are displayed on the screen.

Your roll number is 19
Your name is David
Your marks is 89.8

To explain how this happens, let’s consider following statements:

int rollno;

This line indicates the variable's name is rollno. A variable is a named storage location in the computer's memory used to hold data. Variable must be declared before they can be used. A variable declaration tells the compiler the variable's name and the type of data it will hold. In this statement int stands for integer so rollno will only be used to hold integer numbers.
Similarly, name will be used to hold text string and marks will be used to hold real numbers.

rollno = 19;

This is called an assignment statement. The equal sign is an operator that stores the value on its right into the variable named on its left. After this line executes, the rollno variable will contain the value 19.
Similarly, name will contain David and marks will contain 89.8.

System.out.println("Your roll number is " + rollno);

The println() method prints the characters between quotes to the console. You can also use the + operator to concatenate the contents of a variable to a string.

Literals

A constant value in Java is created by using a literal representation of it. A literal can be used anywhere a value of its type is allowed. In above program, we have used following types of literals

Literal Type of Literal
19 Integer literal
"David" String literal
89.8 Double literal (real number)
"Your roll number is " String literal
"Your name is " String literal
"Your marks is " String literal

 

Identifiers


Identifiers are names of things, such as variables, constants, and methods, that appear in programs. Identifiers must obey Java’s rules for identifiers.


Rules for identifiers


A Java identifier consists of letters, digits, the underscore character (_), and the dollar sign ($) and must begin with a letter, underscore, or the dollar sign.
Identifiers can be made of only letters, digits, the underscore character (_), and the dollar sign ($); no other symbols are permitted to form an identifier.
Java is case sensitive—uppercase and lowercase letters are considered different.
Identifiers can be any length.

The following are legal identifiers in Java:
length
circleRadius
num1
$loan

Examples of Illegal Identifiers Illegal Identifier Description
first Name There can be no space between employee and Salary.
mail@ The @ mark cannot be used in an identifier.
first+second The symbol + cannot be used in an identifier.
1st An identifier cannot begin with a digit.