Java member initialization

Source: Internet
Author: User

Java does its best to ensure that all variables are initialized correctly before they are used. If it is defined as a "local" variable relative to a method, the guarantee is expressed through a compile-time error message. Therefore, if you use the following code:

void F () {
int i;
i++;
} The

will receive an error message telling you I may not have been initialized. Of course, the compiler can also give me a default value, but it looks more like a programmer's fault, at which point the default will be "counterproductive." If a programmer is forced to provide an initial value, it can often help him or her to correct "bugs" in the program.
However, if you set the base type (the primary type) to be a data member of a class, the situation becomes slightly different. Since any method can initialize or use that data, it may not be practical to force the programmer to initialize it to an appropriate value before the data is formally used. However, it is also very unsafe to give it a garbage value. Therefore, all the base type data members of a class are guaranteed to get an initial value. You can see these values in the following applet:

 

: Initialvalues.java
//shows default initial values

class measurement {
  Boolean t;
  char c;
  byte B;
  Short S;
  int i;
  Long L;
  float F;
  Double D;
  void print () {
    System.out.println (
      "Data type      inital value\n" +
      "Boolean        " + t + "\ n" +
      ") Char           "+ C +" \ n "+
      " byte           + B + "\ n" +
      "short          " + S + "\ n" +
      "int            " + i + "\ n" +
   
     "long"           + L + "\ n" +
      "float          " + f + "\ n" +
      "double         " + D);
  }

public class Initialvalues {public
  static void Main (string[] args) {
    measurement d = new Measurement ();
    D.print ();
    /* In the case you could also say:
    new Measurement (). print ();
    */
  }
} ///:~
   


Enter the results as follows:

Data type      inital value
Boolean        false
char
byte           0
short          0
int            0
Long           0
float          0.0
double         0.0


Where the char value is null (NULL) and no data is printed.
As you'll see later, when you define an object handle inside a class, that handle gets a null value if you don't initialize it as a new object.

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.