Reputation: 32767
What is the difference between initialising to null
or empty:
Type name = null;
Type name;
Is there any difference at all?
I understand that for example:
String name = null;
String name = "";
The difference would be that the first one does not initialise it to any value and the other one does, but is there are any difference between the first two?
Upvotes: 1
Views: 129
Reputation:
No. There is no difference. If you do not assign a default value, the variable is automatically considered null
.
Upvotes: 2
Reputation: 285405
The only difference that I know of is if you declare the variable in a local scope and not in class scope, the compiler will complain if you try to use the variable before some initialization for the latter. Otherwise there is no difference that I know.
Upvotes: 2
Reputation: 240870
Type name = null;
Type name;
default value for any reference variable is null
so no difference
if it is defined into some local scope then it will give you compiler error if you attempt to access it before initializing it
Upvotes: 7