The initial value of the Java variable

Source: Internet
Author: User

If a variable in Java is not assigned, the member variable is initialized by default and the local variable does not.

For member variables int A; The initial value of a is 0

The member variables in the following example A,b,c,d

public class Variableinitialvalue {public static void main (String args[]) {testvariable obj = new testvariable (); System.out.println ("a=" +obj.a); System.out.println ("b=" +obj.b); System.out.println ("c=" +obj.c); System.out.println ("d=" +OBJ.D);} Class TestVariable {int A;char b;float C; String D;}

The output is:

For the local variable int A; A default is not initialized

The Java compiler will give you an error warning if you are manipulating variables that are not initialized.

The local variable, a, B in the following example

public class Variabledeclaration {public static void main (String args[]) {System.out.println ("Examples of variable Declar ation "); int A; System.out.println ("a=" +a); String b; System.out.println (b);}}

Error message:

The local variable a May is not having been initialized

The local variable B may not have been initialized

The initial value of the Java variable

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.