I'm very confused between the two terms. I checked on stackoverflow and there's a similar question for C++ but not for java.
Can someone explain the difference between the two terms for java?
I'm very confused between the two terms. I checked on stackoverflow and there's a similar question for C++ but not for java.
Can someone explain the difference between the two terms for java?
Declaration: The declaration concept includes notifying the compiler about properties of the variable such as its name, type of value it holds and the initial value if any it takes.
Definition of a variable says where the variable gets stored. i.e., memory for the variable is allocated during the definition of the variable.
public class JavaDemo{
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
}
}
I just wanted to reiterate that @stephen-c's answer was excellent. However, I believe that it needs a slight tweak. The JLS does use the term "definition" outside of its natural-language meaning in a few places. I believe that this only confuses the otherwise clean language in the specification that focuses the reader's attention on declarations instead of definitions. Consider the bottom of example 15.12.2-1.:
If a third definition of test were added:
static void test(ColoredPoint p, ColoredPoint q) {
System.out.println("(ColoredPoint, ColoredPoint)");
}
then it would be more specific than the other two, and the method invocation would no longer be ambiguous.
Again, I wish that they had not included these few errant uses of definition which would have helped their focus on declaration instead of definition that much more obvious.
Oh well.