When declaring any primitive type data like int or double they get initialized to 0 or 0.0. Why can we not set them to null?
When declaring any primitive type data like int or double they get initialized to
Share
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.
A primitive type is just data. What we call objects, on the other hand, are just pointers to where the data is stored. For example:
In this case,
objectis just a pointer to an Integer object whose value happens to be 3. That is, at the memory position where the variable object is stored, all you have is a reference to where the data really is. The memory position wherenumberis stored, on the other hand, contains the value 3 directly.So, you could set the
objectto null, but that would just mean that the data of that object is in null (that is, not assigned). You cannot set an int to null, because the language would interpret that as being the value 0.