Skip to main content

JAVA: Variables.

JAVA.

What is a variable? what are the types of variable? To know the complete details about constant,learn the following carefully and completely.

Variables.

    Identifiers are also called as variables in Java programming.

    They are  used to name a memory location.

    The value of the variable can be changed during the execution of the program.

Declaration of variable.

    Variable declaration consists of data type followed by one or more variable name and ending with a semicolon(;).

    This variable declaration tells compiler what kind of data type it has.

General syntax:
     datatype  variable1,  variable2..... variable N;


Rules for declaring variable.

  • First Character must be an alphabet followed by numbers or letters.
  • Special characters are not allowed except underscore (_) sign.
  • It cannot be keyboard.
  • They can be of any length.
  • It is case sensitive, that is uppercase and lowercase letters are distinct.
Types of variable
  • Integer variable
  • Float variable
  • Double variable
  • character variable
  • String variable
Integer variable
         A number without fraction is called integer. This variable takes only integer values.
   Syntax
       int  variable1, variable2.....variable N;
   Example
        int subject 1, subject 2, total;

Float variable
       A number with fraction part is called float-point. This variable will also deal with fraction part of numbers.
    Syntax
     
          float  variable1, variable2.....variable N;
    Example
          float salary, average;

Double variable
        A number with more precision in fraction part is called double floating point. Interest double variable deals with precision in fractions.
    Syntax
           double  variable1, variable2.....variable N;
    Example
           double salary, average;

Character variable.
    A character is single character closed the with single quotation.
    Syntax
       char variable1, variable2.....variable N;
    Example
       char gender, choice;

String variable.
   String is a group of character enclosed with double quotation.
     Syntax
       string variable1, variable2.....variable N;
     Example
        string name, address;


Initialization of a variable.
        The process of assigning value to a variable at the time of declaration is known as initialization

Types of initialization.

  • Static initialization
  • Dynamic initialization

Static initialization.
        When variable gets initialized during declaration is termed as static initialization. Under this initialization a variable is assigned directly with assignment operator.

Dynamic initialization.
        A variable get initialized during runtime is  termed as dynamic initialization. Under this initialization the value is assigned with any arithmetic or logical operations.



Giving value to variables.
     Every variable must be given a value after it has been declared but before it is used in an expression. 

This can be achieved in three ways. 

  • By using assignment statement.
  • By using stream reader.
  • By using command line argument.

By using assignment method.
    Simple method of giving value to a variable is through the assignment  statement.

By using stream reader.
    This is a user-friendly statement where you can accept the value of the console at the time of execution of the program.

    Declaring a java library package at the beginning of the program
                    Import java.io.*;
    Declaring buffer to store the data value at the time of execution along with IO exception with the main().
       Input stream reader read =  input stream reader( system.in);

By using command line argument.
    Command line arguments are parameters that are supplied to the application program at the time of invoking it for execution.


Scope of variable.

      Scope of a variable determines the area in which a variable can be assigned.  

Java variable is actually classified into three kinds.

  • Instance variable.
  • Class variable.
  • Local  variable.

Instance variable.
   This variable are declared in a class but outside a method, constructor or any block.

Class variable.
   Class variable are also known as static variables. They are declared within the static keywords in a class but outside a method, constructor or a block.

Local variable.
Local variable are declared in method constructor or a blog.



Check out the interesting history of Java.

The important features of java.

The structure of java.

The environment of java.

Also the complete process to execute java program.

check out other tokens:

                identifier.
               keywords.
               constants.
               datatypes.

Comments