What is the different between:
Socket connection = null;
and
Socket connection;
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If it’s a field (static or instance variable) then the two are broadly equivalent as
nullis the default value. (There’s a subtle difference if a super-constructor calls an overridden method which sets the value, but that’s a corner case.)If it’s a local variable, they’re very different – in the first form, the local variable is definitely assigned, and the value can be read in the very next statement. In the second form, the variable isn’t definitely assigned, and you won’t be able to read it from code until such a point where the compiler can prove that a value will have been written: