In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the detailed introduction of the Java keyword null", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the detailed introduction of the Java keyword null"!
1. Null represents an uncertain object
In Java, null is a keyword that identifies an uncertain object. Therefore, you can assign null to reference type variables, but you cannot assign null to base type variables.
For example, int a = null; is wrong. Ojbect o = null is correct.
In Java, the application of variables follows the principle that they are defined and initialized before they can be used. We can't print the value of an after int a without assigning a value. This also applies to reference type variables.
Sometimes, we define a reference type variable, at the beginning, can not give a definite value, but do not specify a value, the program may initialize the value in the try statement block. At this point, we will report an error when we use variables next. At this point, you can first specify a null value for the variable, and the problem is solved. For example:
Connection conn = null; try {conn = DriverManager.getConnection ("url", "user", "password");} catch (SQLException e) {e.printStackTrace ();} String catalog = conn.getCatalog ()
If conn = null is not specified at the beginning, an error will be reported in a single sentence.
Null itself is not an object, nor is it an instance of Objcet
Null itself can represent an uncertain object, but as far as null itself is concerned, it is not an object, it does not know what type, and it is not an instance of java.lang.Object.
You can make a simple example:
/ / is null an object? Is it an Object type? If (null instanceof java.lang.Object) {System.out.println ("null is of type java.lang.Object");} else {System.out.println ("null is not of type java.lang.Object");}
The result will output: null is not of type java.lang.Object
3. Java assigns values to variables by default
When defining a variable, if the variable is not assigned a value after the definition, Java automatically assigns a value to the variable at run time. The assignment principle is that the automatic assignment of integer types int, byte, short and long is 0, the automatic assignment of float and double with decimal point is 0, the automatic assignment of Boolean is false, and the automatic assignment of other referenced type variables is null.
This specific can be seen through debugging.
IV. Container types and null
List: repeating elements are allowed, and any number of null can be added.
Set: duplicate elements are not allowed, you can add at most one null.
The key of Map:Map can be added to at most one null,value field without limit.
Array: an array of primitive types that, after definition, is automatically given by the java runtime if no initial value is given. Reference type array, no initial value is given, then all element values are null.
Other functions of null
1. Determine whether a reference type data is null. Judge by =.
2. Free up memory so that a non-null reference type variable points to null. In this way, this object is no longer applied by any object. Wait for the JVM garbage collection mechanism to collect.
At this point, I believe you have a deeper understanding of the "detailed introduction of the Java keyword null". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.