Hello!
What is the difference between defining and declaring a variable in java?
Regards,
Hello!
What is the difference between defining and declaring a variable in java?
Regards,
The basic types of variable in C are namely char, int, double and float. First the variables have to be declared. This is done as follows:
First any of the data type defined above must be mentioned and this is followed by the name of the variable. For instance if wants to declare a variable v1 of type char it is done as follows
char v1;
While declaring a variable what happens is the data type is described to the compiler and no space or memory allocation takes place. In other words just the data types are mentioned without any memory storage for the data types. While defining the variable the declaring the data type of the variable along with space allocation for the variable takes place.
Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.
In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization.
e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.
The following is a variable declaration:
int x;
The following is a variable definition:
int x = 10;
The following are method declarations:
abstract void foo();
native void foo();
method definitions is when what the method does is defined:
void foo() {
System.out.println("Hello");
}
In interfaces you can define variables (but they are really all constants). In interfaces you can only declare methods, but not define them.
In abstract classes, some methods may be only declared and others declared and defined.
Bookmarks