Java variable initialization assignment null

Source: Internet
Author: User

in Java, a null value indicates that the reference does not point to any object. When the system discovers that a reference is used during the run, it can immediately stop further access without any risk to the system. 1If it is the field of the object, then the system assigns the values of those objects to null when initializing the object, and the basic data types have their initial values.2if it is within a method, then the programmer needs to assign values to the variables themselves. classt{Object obj=NULL; Object obj1;//The system automatically assigns null to him at initialization time .}classt{ Publicvoidx () {Object obj=NULL;//This needs the programmer to do it himself.}} The variables in the class do not have to be initialized with local variables in the initialization method
one, NULL is an indeterminate object in Java, and null is a keyword used to identify an indeterminate object. Therefore, you can assign null to a reference-type variable, but you cannot assign null to a primitive type variable. For example:intA =NULL; it's wrong. Ojbect o =NULL is correct. In Java, the applicability of variables follows a principle, defined first, and initialized before it can be used. We cannot assign a value to a after int a, and then print the value of a. This pair is also applicable for reference type variables. Sometimes, we define a reference type variable that, at the beginning, cannot give a definite value, but does not specify a value, and the program may initialize the value in a try statement block. At this point, we will use the variables below the error. At this point, you can assign a null value to the variable before the problem is resolved. Example: Connection conn=NULL; Try{conn= drivermanager.getconnection ("url", "User", "password"); } Catch(SQLException e) {e.printstacktrace (); } String Catalog=Conn.getcatalog (); If you don't specify Conn at first=NULL, the last sentence will be an error. The null itself is not an object, nor is it an instance of Objcet. Null itself, although it can represent an indeterminate object, but null itself, it is not an object, do not know what type, nor is it an instance of Java.lang.Object. You can do a simple example://is null an object ?if(NULL instanceofjava.lang.Object) {System.out.println ("Null belongs to Java.lang.Object type"); } Else{System.out.println ("Null does not belong to Java.lang.Object type"); The result is output: null does not belong to Java.lang.Object type Three, java default to variable assignment when defining a variable, if you do not assign a value to the variable, Java will automatically assign a value to the variable at run time. The assignment principle is an integer type int,byte、 Short, long is automatically assigned a value of 0, float with a decimal point, double is automatically assigned to 0.0, the Boolean automatically assigns a value of false, and the other reference type variables are automatically assigned null. This can be seen through debugging. Iv. container type and null List: Allows repeating elements, can join any number of NULL. Set: Duplicate elements are not allowed and a maximum of one null can be added. The Map:map key can be joined to a maximum of one null,value field without restrictions. Array: An array of primitive types that, when defined, is automatically given a value by the Java runtime if no initial value is given. An array of reference types, not given an initial value, all element values are null. V. Other effects of NULL1. Determine if a reference type data is null. with = =to judge. 2. Free memory, and let a non-null reference-type variable point to null. This way the object is no longer applied by any object. Wait for the JVM garbage collection mechanism to recycle.

Java variable initialization assignment null

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.