Java Programming
About Lesson

DECLARATION AND INITIAISATION OF VARIABLES

Variables:

Variable represent memory location in which value can be stored.

3 kinds of variables in java:

  1. Instance variables: used to define the attributes of a particular object
  2. Class variables: their values apply to all the instances of a class rather than having different values for each object
  3. Local variables: declared and used inside methods

The declaration of variable is free to put anywhere in the program, but before its first use.

Rules for naming a variable:

Variable name cannot start with any symbol other than _ or $

Java is case sensitive

variable name can’t contain a white space

there is no limit for the length of variable name

Variable can be initialized at the time of declaration or dynamically.

Example program that initialize the variable dynamically:

/*program that calculates the hypotenuse of a triangle based on its height ‘h’ and base ‘b’, using formula : square root of [h2+b2]*/

class DynamicInitialization

{

public static void main(String args[])

{

double h=3.0, b=4.0;

double c= Math.sqrt(h*h+b*b);         //Dynamic Initialization

System.out.println(“Required Hypotenuse is”+c);

}

}

You cannot copy content of this page