Variables and Data Types in Java

Variable is a kind of box that stores value. This box or space is a part of memory that contains data value. Before declaring and initializing variables let us see some rules to write them.

Rules to write Variable names in Java

It is always better to choose a  name that describes the purpose of variable to the reader. Say you want to store marks of a student in a variable then variable names could be marks or marksStudent.

  • All variable names should begin with a letter of the alphabet, underscore(_) or a dollar($) sign. It is highly recommended to use letter of the alphabet instead of $ or _. For e.g. marks, average, grade,etc. Refer Fig 1.


Fig 1
  • No spaces or special characters are allowed. Name may contain letter or digits(0-9) after the first letter. For examples refer to Fig 2.
Fig 2
  • There is no restriction on the length of variable name. But it is we have to type, so please try to keep it short and descriptive.
  • Variable names are case sensitive. It means variable marks is different from variable Marks.
  • Use of java keywords like catch, char, double, int etc. are not allowed.
  • Never use uppercase for every letter in a variable as it is reserved for final (constants) variables.

Declaration of a Variable

int rollNo = 23;

rollNo is a kind of box that stores 23 which is value at some memory location. int is a type of data associated with value i.e. 23. int is a data type that variable rollNo can store whole numbers. Depending on the type of data we want to use, different data types are available.

data type variable_name;


double average;

Initialization of a Variable

double average = 34.6;

Data Types in Java

There are 8 primitive data types in Java. They are:

  1. int
  2. float
  3. double
  4. char
  5. boolean
  6. short
  7. long
  8. byte
Fig 3 : Data Types In Java



package video_tutorial_1;
public class DataTypes 
 public static void main(String [] args)
 //declaration of variables
 // integer  takes whole numbers as 32 bit signed values
 int rollNumber;
 // double stores 64 bit floating point value
 double average;
 // byte stores 8 bit signed value
 byte age;
 //float stores 32 bit signed value
 float height;
 // short stores 16 bit signed value
 short classSection;
 //char stores 16 bit Unicode character
 char grade;
 //long stores 64 bit signed value
 long totalMarks;
 //boolean stores binary value either true or false
 boolean passStatus;
 //initialization of variables
 rollNumber = 56;
 average = 23.4;
 age = 30;
 height = 6.2f;
 classSection = 8;
 grade = 'A';
 totalMarks = 1910;
 passStatus = true;
 //print variables
 System.out.printf("The roll number of student is: %d \n", rollNumber);
 System.out.printf("The average of marks is: %f \n", average);
 System.out.printf("The age of student is: %d \n", age);
 System.out.printf("The height of  student is: %f \n", height);
 System.out.printf("The class section of student is: %d \n", classSection);
 System.out.printf("The grade of student is: %c \n", grade);
 System.out.printf("The total marks os student are %d\n", totalMarks);
 System.out.printf("The pass status of students is : %b\n", passStatus);



Rate this post


So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *