Data types and Variables

 Datatype:
    Specifies the size and then type of values that can be stored in an identifier.

    They are used to represent how much memory is required to hold the data.


Variables: 
    Variables are nothing but a name given to storage area.

Three types of variables:
    1. Local Variable
    2. Instance variable
    3. Static variable


1. Local Variable: local variable in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists. If we are declaring a local variable then we should initialize it within the block before using it.


2. Instance variable:
The variables which are declared inside a class outside any method /block/ a constructor are known as instance variables.


     For Instance variables JVM will automatically assign them with their default values.

We can access instance variables in 2 ways, by using identifier name & by using class instance / class object.

For instance variables memory will be assigned at the time of creating a class instance/object.

*For instance of a class there will be a separate copy of instance variables will be created.




3. Static variable: 

The variables which are declared as class ouside any method or a block or a constructor with static keyword are known as static variables.

* For Static variables JVM will automatically asign them with their default values.


* We can access a static variable in 3 ways- by using identifier name, by using class instance, by using class name.


* There will be only one copy of static variable available throughout the program.


* For final static variable JVM will not provide any default values, it 
is the responsibility of the programmer to initilize that variable, otherwise we will be getting compile time errors.

Comments

Popular posts from this blog

OOP's Concepts

Basics about Core Java