In short Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of “vary + able” that means its value can be changed. In this article we discus about variables and data types in java.

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Type of Variables: There are three (3) types of variables in java:types-of-variables-java

  1. Local Variable : A variable which is declared inside the method is called local variable. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block. Access modifiers cannot be used for local variables.
  2. Instance Variable : A variable which is declared inside the class but outside the method, is called instance variable . Instance variables are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created. Access modifiers can be given for instance variables.
  3. Class/Static Variables : A variable that is declared as static is called static variable. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value. Static variables can be accessed by calling with the class name ClassName.VariableName.


Type of Data: Data types represent the different values to be stored in the variable. In java, there are two (2) types of data types:Data Types in Java

  1. Primitive data types: There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.
    bytebyte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer. byte data type is an 8-bit signed two’s complement integer
    shortshort data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer. short data type is a 16-bit signed two’s complement integer
    intinteger is generally used as the default data type for integral values unless there is a concern about memory. int data type is a 32-bit signed two’s complement integer.
    longlong type is used when a wider range than int is needed. long data type is a 64-bit signed two’s complement integer.
    floatfloat is mainly used to save memory in large arrays of floating point numbers but never used for precise values such as currency.float data type is a single-precision 32-bit IEEE 754 floating point.
    doubledouble data type is generally used as the default data type for decimal values, generally the default choice but never be used for precise values such as currency. double data type is a double-precision 64-bit IEEE 754 floating point.
    booleanboolean data type represents one bit of information. This data type is used for simple flags that track true/false conditions. There are only two possible values: true and false.
    charchar data type is used to store any character. char data type is a single 16-bit Unicode character
  2. Non-primitive (Reference/Object) data types: Non-primitive data types are created by programmer. It is also called as ‘Reference Variables’ or ‘Object reference’ because it refers a memory location where data is stored. On the other hand, primitive data types stores value.

Why string is not primitive data type in java??
String is not a primitive data type because it has methods and only class can have methods. ‘String’ is used to represent string class. Primitive data types cannot have methods. So, String is a class which is non-primitive type. It is class in java provided with several methods used to store group of characters.

Thanks for sharing Variables and Data Types in Java.

Variables and Data Types in JavaVariables and Data Types Variables and Data Types

Leave a Comment