Difference between Definition and Declaration in Java.

Java 8Object Oriented ProgrammingProgramming

For the difference between definition and declaration, one should consider their literal meaning first which includes Declare means to announce or proclaim while Define means to describe some entity.

The following are the important differences between the Definition and the Declaration.

Sr. No.KeyDeclarationDefinition
1ConceptThe concept of declaration includes informing the compiler about properties of the variable such as its name, type of value it holds and the initial value if any it takes.While the definition is basically the actual implementation and memory location of function and about memory for the variable is allocated during the definition of the variable.
2Exception in CBoth declaration and definition take place at the same time in the case of c language.In other languages such as Java both occur at different places.
3Number of occurrencesThe declaration could be done multiple times either of a variable or of function.Variable or function could be defined only once.
4Memory allocationMemory has not been allocated during the declaration of a variable or function.Memory has been allocated during the definition of a variable or function.

Example of Declaration vs Definition

JavaTester.java

 Live Demo

public class JavaTester{
   public static void main(String args[]){
      int a; // declaration of variable
      a=10; // definition of variable
      functionA(a); // declaration of function
   }
   public static void functionA(int a){
      System.out.println("value of a is " + a); // definition of function
   }
}

Output

value of a is 10
raja
Published on 18-Sep-2019 12:12:09
Advertisements