Java Variable default value

Source: Internet
Author: User

The Java language requires that variables follow the rules that are first defined, then initialized, and then used. The initialization of a variable is the first time it is assigned an initial value since the definition of the variable.

First, member variables

The JVM assigns default values (the default) to the instance and static variables of the class, including each element in the array arrays--without having to write the initialization assignment statement. The final variable has no default value and must be assigned before the constructor ends.

The default values are as follows:

1, the integer type (byte, short, int, long) is the default value of 0.
2. The default value of single-precision float (float) is 0.0f.
3, double-precision floating-point type (double) default value is 0.0d.
4, the character type (char) defaults to "/u0000".
5, Boolean default value is False.
6, the reference type default value is null.
7, the array reference type default value is null. If no values are displayed for each element, Java initializes all elements of the array to their corresponding type's default values.


This default assignment is done before the object's constructor call, if the program writes an assignment to the instance and static variables, and the value given is the default value of the JVM, then it is undoubtedly superfluous and repeated work again.

Situation One:

public class Foo {   private int count=0;   Redundant   private static Boolean dd=false;//Extra Public    Foo ()    {        super ();    }      }
situation Two:

public class Foo {   private int count;   private static Boolean DD;    Public Foo ()    {        super ();        count=0;   Excess        dd=false;   Superfluous            }      }

second, local variables

A local variable has no default value and must be initialized manually!

If the compiler confirms that a local variable may not be initialized before it is used, the compiler will make an error.

Note: If the local variable is not initialized and has not been used in the method, both the compilation and the Run pass.

public void Method () {int a;a++;//compile error, variable a must initialize System.out.println (a);}


Java Variable default value

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.