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
|
|
Run Output
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
Created: 20-Mar-2018 16:27 | Updated: 21-Mar-2018 19:04|
