Friday 13 January 2017

Variables in Java


                                              
Variables in Java
 

 Variables 


 A Variable may be defined by the combination of an identifier , a type , and an optional initializer. every variable defined has a scope , has a set visibility and lifetime.


Declaring a Variable


In java , the variables must be declared before it can be used. the variables are defined as follow :

     type identifier = value ;

                        Here type is one of the atomic types or class name or interface. The identifier is the name of variable.

You can initialize a variable by specifying an equal sigh and a value.


Dynamic Initialization


 Java allows the variables to be initialized dynamically. for Ex

  // Demonstrate dynamic intialization

  class DynInit
  {
         public static void main ( String args [])
    {
              double a=3.0 , b=4.0 ;

 // c is dynamically initialized

  double c = Math.sqrt ( a * a + b * b );

System.out.println ( "Hypotenuse is =" + c );
     
     }
}

 Here 3 local variables a ,b,c are declared . the first 2 , a and b are initialized by constants . However c is initialized dynamically.

The program uses java's inbuilt methods sqrt()v, which is the member of Math class , to compute the square root of argument.

                                                              By: Knowledge Bits   
       

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *