Programming





What is the difference between variable declaration and variable definition?

What is the difference between variable declaration and variable definition?


by Administrator | 25-Mar-2020
Java

Tags:  Core Java  Declaring  Defining  Variable

Share: 


Declaring: In declaration, we just mention the type of the variable and its name. It gives the information to the compiler about the size of the variables. We do not initialize* it. For example:

int size;
String title;

Defining: Defining means declaration and initialization*. It tells the compiler about the space as well as the memory required by the variable. For example:

int size = 10;
String title = "programming without the vowels";

We cannot start using a local variable if we don’t initialize it. But instance variables can be used even if we don’t initialize them because the instance variables are provided with a default value. Here are the default values for various data types:

Data type

Size required

Default value

boolean

Unspecified/JVM Specific

false

char

2 (16 bits)

ASCII-0

Unicode-\u0000

byte

1 (8 bits)

0

short

2 (16 bits)

0

int

4 (32 bits)

0

long

8 (64 bits)

0L

float

4 (32 bits)

0.0f

double

8 (64 bits)

0.0d

 

*Initialization is assigning the variable with a default value.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.