Variable Declaration and Initialisation – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3862    siteicon   siteicon  

Variable Declaration and Initialisation

Variable declaration and initialisation varieties are explained here.

Learning Objectives

  • Understanding variable declaration.
  • Declaration along with initilisation.
  • Separate Assignment after declaration.
  • Assignment with a returned value of method.

Source Code

TC++ #3862

Source Code

class VarDecInit
{
public static void main(String[] args)
{
int x;
x=10;
int y=20;
int z=y;
double d=Math.pow(2,3);
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(d);
}
}

Test it !

Run Output

10
20
20
8.0

Code Understanding

int x;
This is plain variable declaration. No value has been assigned

x=10;
This is assignment statement only which works on pre-declared variables only.

int y=20;
This is declaration combined with assignment which is also called initialisation.

int z=y;
This is declaration and initialisation of a variable with another pre-assigned variable.

double d=Math.pow(2,3);
This is declaration and assignment of a variable with return value of a method. The return value should of given variable data type.

Notes

  • A variable can assigned with –
    – A Literal value of given data type.
    – A reference to another variable which should be of given data type or possibly convertible data type
    – A call to a method which returns the value of given data type. Some times this is also called dynamic initialisation.
  • The oracle documentation for variable and other definitions can be seen at.
    https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html


Suggested Filename(s): VarDecInit.java



Share

sunmitra| Created: 20-Mar-2018 | Updated: 21-Mar-2018|






Back