Variables - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Variables - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, March 14, 2019

Variables

Variables


üVariable is a name for a location in memory.

üDeclaring a variable:

  type identifier [=value][,identifier [=value]….];

üThe initialization expression must result in a value of the same 
or compatible type as that specified for the variable.

üWhen a variable is not initialized, the value of that variable is 
undefined.

Scope and Lifetime of a variable




üA block begins with an opening curly brace and ends by a 
closing curly brace.


üA block determines scope, that defines which objects 
are visible to other parts of your program.

üVariables declared within a block localize themselves.

üIn case of nested block, the outer block encloses the
 inner block. The variables declared in the outer block 
is visible to the inner block but the reverse is not true.

üA variable will not hold it’s value once it has gone out 
of it’s scope.

üIn an inner block, it is not possible to declare a 
variable of the same name as in outer block.

Scope and Lifetime of a variable

Example:

public static void main( String args[])

{

  int x =10;

  if ( x == 10)

  {

  int y =20;

  System.out.println(“x and y: “ +x +” “+y);

  x= y * 2;

  }

  y= 100;  //Error

  System.out.println (“x is “+x);

}

No comments:

Post a Comment

Post Top Ad