Introduction to Variables
Variables are used to store values or information of a program during the program execution. The value can be changed at any point in the program—hence the name.
When you perform some tasks in a program and get data as a result, you need to store the data at some place so that you can get it later on. At that time, variables are used to hold the data. You can get previously stored data from variables at any point during the program execution.
To create a variable, you must give it a name (identifier) and identify what type of information (data type) it will store. You also can give a variable an initial value at the same time you create it.
There are three kinds of variables in Java: instance variables, class variables, and local variables.
- Instance variables: Objects store their individual state by their instance variables.
- Class variables: define the attributes of an entire class of objects and apply to all instances of it. Variables of a class are also called data members or properties.
- Local variables: are used inside method definitions or even smaller blocks of statements within a method. You can use them only while the method or block is being executed by the Java interpreter.
Syntax
Datatype variableName;
OR
Datatype variableName = value;
OR
Datatype variableName1 = value, variableName2 = value, variableName3 = value;
Data type means what kind of data a variable can store. Variable name is identifier which is used to access and assign value to the variable.
There are three types of variable creation. In first type, we only declare variable. In second type, we declare and initialize a variable with some value in single statement. In third type, we create multiple variables and initialize them in single statement.
Assignment operator “=” is used to assign value to the variable.
Example
1. /**
2. * @(#)Example.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/10/26
7. */
8. public class Example {
9. /**
10. * Creates a new instance of Example.
11. */
12. public Example() {
13. }
15.
16. /**
17. * @param args the command line arguments
18. */
19. public static void main(String[] args) {
20. // TODO code application logic here
21.
22. System.out.println("Student Information"); // simple message
23.
24. String name = "Saghir Qasim"; // this declares and initialize a variable name
25. System.out.println("Name: "+name);
26.
27. char gender = 'M'; // this declares and initialize a variable gender
28.
29. System.out.println("Gender: "+gender);
30.
31. String program = "MCS"; //this declares and initialize a variable program
32.
33. System.out.println("Program: "+program);
34.
35. int semesters; // this declares a variable called num
36. semesters = 6; // this assigns num the value 100
37.
38. System.out.println("Semesters: " + semesters);
39.
40. int subjects; // this declares a variable called num
41. subjects = 5; // this assigns num the value 100
42.
43. System.out.println("Subjects: " + subjects);
44.
45. int totalSubjects = semesters * subjects; // multiply semesters and subjects and store
result in totalSubjects variable
47.
48. System.out.println("Total subjects: " + totalSubjects);
49.
50.
51. float gpa = (float) 3.65;
52.
53. System.out.println("GPA: "+gpa);
54. }
55. }
Output
Student Information
Name: Saghir Qasim
Gender: M
Program: MCS
Semesters: 6
Subjects: 5
Total subjects: 30
GPA: 3.65
Data Types
In a Java program, data type of a variable tells us that what kind of information the variable can hold.
The data type can be any of following:
- Primitive Data Types: In Java programming language char, Sting, byte, short, int, long, float, double, boolean are built-in or primitive data types.
- Class or Interface: In Java, a class or interface is user defined data type which consists of attributes (data members) and behavior (methods).
- An Array: Array is also a data type. An array is used to store multiple values of same data type in a variable.
The int data type is used to hold numbers. They are signed means they can store positive and negative values. Each variable occupies memory space where it stores its value so the variable cannot store less than or more than of its size.
| Type | Size | Values that can be stored |
| byte | 1 byte | -128 to 127 |
| short | 2 byte | -32,768 to 32,767 |
| int | 4 byte | -2,147,483,648 to 2,147,483,647 |
| long | 8 byte | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
The float or double is used to store decimal point value i.e. 35.50. Char data type is used to hold a single character, number, punctuation or symbol etc. If you want to store series of characters means a string, you need String data type.
The boolean data type is used to store true or false.
A class is a user defined data type which is used to describe the properties (data members) and behavior (methods) of a real world entity. For example, Car is a user defined data type (class) which holds properties and behavior of a car.
An array is a variable which holds collection of values of same data type. Each value can be accessed by its associated index.
