Default Value of local variables

Source: Internet
Author: User

Default Value of local variables
I read several questions from Java certified engineers yesterday. One question is as follows.
The default value of the local variable Boolean is
A, false
B. True
C. null
D. No default value. Initialization is required.
The correct answer is D. At that time, my answer was a false. Why should I select a? The reason is as follows.
Default Value of basic type
Boolean false
Char '\ u0000' (null)
Byte (byte) 0
Short (short) 0
Int 0
Long 0l
Float 0.0f
Double 0.0d

However, my answer is wrong. I typed a similar code in eclipse manually, tested it, and proved again that the answer D is correct. The specific reasons are as follows.
The system does not automatically assign initial values to local variables, but the system automatically assigns initial values to member variables. The value of the basic type is 0, and the value of the composite type is null. Therefore, the default value of the above type is only used for member variables and not for local variables.

Last modified: 2011-12-25

For more technical articles, visit thinkblog.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.