Basic Java – 2 || Variables and Data Types used in JAVA.

            As the name sounds “Vary+able”, means the value can be changed. Basically, the variable is the name of the memory location which is used to store and retrieve the value in it.

Related Links:

OOPs Concept:

Java Question And Answer:

Definition and Types of Variable

          Variables are reserved memories to store values in RAM. These memories get released as soon as the life of variable gets finished. Based on the life of variables there are three types of Variable.
1. Local Variables: Life remains within a block where it has been declared.
2. Instance Variables: Declared inside the Class but outside the methods. It shouldn’t be static.
3. Static Variables: It’s like a Global variable. Declared as static in the class but outside the methods.

public class Variables {
       int instanceVar=5;
       static int staticVar=8;

       public static void main(String[] args) 
               int localVar=10;
               System.out.println("Loacal Variable:"+localVar);
      Variables obj=new Variables();
      System.out.println("Instance Variable: "+obj.instanceVar);
               System.out.println("Static Variable: "+staticVar);

         In the above example, the life of localVar will be within the main() method only. If we try to use the same variable outside the main method it will throw an error.

instanceVar can be accessed only through objects of the ‘Variables’ class.

staticVar can be used without any object and can be used only in the static methods.

Variables Naming Convention

The variable name should start with letters, $ and _. We can’t use any other character as well as java reserved keywords.
Valid example:

Invalid example:
void (Java reserved Keyword)
int (Java reserved Keyword)
static (Java reserved Keyword)

Data Types in JAVA

             Simply data types tell the compiler type of variable. It tells the compiler which type of data a variable can store in it. For example, an integer variable can store only numeric values. If we try to store a letter in that variable, the compiler will throw an error.
Basically, there are two types of Data Type:
1. Primitive: Known as primary data types. These data types are not breakable further. These are self-explanatory like boolean, short, int, long, float, char, double, byte.
2. Non-Primitive(Reference): Data type which has been created using the Primitive data types like String, Array, Enum etc.

Escape Sequence

           Java language supports few special escape sequences for String and char literals as well.
The escape sequence is a character which can change the meaning of other characters which follow it.
Most used escape sequence:
\n        Newline (0x0a)
\”        Double quote
\’        Single quote
\\        backslash


Suppose we have to print the following sentence:

Nancy said “Hello World” to the crowd.

print “Nancy said “Hello World!” to the crowd.”;
produces a syntax error, whereas:

print “Nancy said \”Hello World!\” to the crowd.”;  ### example of \”
produces the intended output:

Variable Declaration and Initialization

The declaration is to define a variable and its type, for example:

int rollNumber;

Initialization is to assigning correct value to a variable:


We can combine both the above steps as mentioned below:

int rollNumber=10;

String name=”testData”;  // To store any string value we have to mention it within the quotes.

float myVar=4.5;              // To store float values

boolean flag=true;           // true and false are java reserved keywords.

1432total visits,1visits today

2 thoughts on “Basic Java – 2 || Variables and Data Types used in JAVA.”

Leave a Reply

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