Java and null inextricably linked. It's hard to find a Java-programmer who has not faced aNullPointerException. Even if the author of the concept of a null pointer acknowledged his "error in a billion dollars," why he remained in Java? Null is present in Java for a long time, and I am sure that the language designers know that it creates more problems than it solves.This is surprising, given that the philosophy of Java - to make things as simple as possible. If the developers abandoned the pointers, operator overloading and multiple inheritance, why they left null? I do not know the answer to this question. However, no matter how much criticism is addressed to null in Java, we will have to live with it. Instead of complaining, let's learn how to use it properly. To be careful enough when usingnull, Java will make you suffer by a terrible java.lang.NullPointerException. The most common cause ofNullPointerException - lack of understanding of the intricacies of using null.Let us remember the most important things about it in Java.
Java and
null inextricably linked. It's hard to find a Java-programmer who has not faced aNullPointerException. Even if the author of the concept of a null pointer acknowledged his "error in a billion dollars," why he remained in Java? Null is present in Java for a long time, and I am sure that the language designers know that it creates more problems than it solves.This is surprising, given that the philosophy of Java - to make things as simple as possible. If the developers abandoned the pointers, operator overloading and multiple inheritance, why they left null? I do not know the answer to this question. However, no matter how much criticism is addressed to null in Java, we will have to live with it. Instead of complaining, let's learn how to use it properly. To be careful enough when usingnull, Java will make you suffer by a terrible java.lang.NullPointerException. The most common cause ofNullPointerException - lack of understanding of the intricacies of using null.Let us remember the most important things about it in Java.What is null in Java
As we have found, null is very important in Java. Initially it served to indicate the lack of something, such as a user, resource, and so on. N. But a year later it became clear that he brings a lot of problems. In this article we consider the basic things you should know about null pointers in Java, to minimize check null and avoid nasty NullPointerException.
1. First of all, null - is the key word in Java, as a public, static, or final. It is case sensitive, so you can not write a Null orNULL, the compiler will not understand and will give an error:
This problem often occurs with programmers who are moving to Java from other languages, but with modern development environments this is not essential. These IDE, like Eclipse or Netbeans, correct these errors as you type code. But in times of Notepad, Vim or Emacs was a serious problem, which is time-consuming.
2. Just like any primitive type has a default value (0 in int, false from boolean), null - the default value of any reference type, and hence for any object. If you declare a Boolean variable, it is set to false. If you declare a reference variable, it is assigned the valuenull, regardless of the scope and accessmodifiers. The only thing the compiler will warn you attempt to use an uninitialized local variable. To verify this, you can create a reference variable without initializing it, and bring it to the screen:
This is true both for static and non-static for variables. In this case, we announced myObj as a static variable so that it can be used in static method main.
3. Contrary to popular belief, null is neither the object nor type. It's just a special value that can be assigned to any reference type. In addition, you can also result in null to any reference type:
As you can see, cast null to the reference type does not cause any errors during compilation or at startup. Also, when you start not NullPointerException, despite the common misconception.
4. null can only be assigned to a variable of a reference type. Primitive types - int, double,float, or boolean - the value null assign impossible. The compiler will not allow it and will give an error:
Thus, an attempt to assign a value to null primitive type - compile-time error, but you can assign a null type-wrapper, and then assign it a value corresponding to the primitive. The compiler will not swear, but when the code is thrown NullPointerException. This is due to automatic wrapping (autoboxing) in Java
5. Any object class-wrapper with a value of null throw NullPointerException when deploying(unboxing). Some programmers think that the wrapper automatically assign primitive default value (0 for int, false for boolean, and so on. D.), But it is not:
If you run this code, you'll see Exception in thread "main" java.lang.NullPointerException in the console. This often happens when dealing with HashMap with keys of type Integer. The code below will break as soon as you start:
Conclusion:
This code looks simple and understandable. We are looking for as each number is found in the array, it's a classic way to find duplicates in an array in Java. We take the number of the previous value, increment it and put back into the HashMap. We believe that Integer will make sure to return the default value for int, but if the number is not in theHashMap, the method get () returns null, not 0. And wrap throw NullPoinerException. Imagine that the code is wrapped in a condition and insufficiently tested. Once you start it on Productions - oops!
6. The operator instanceof returns false, when applied to a variable with a value of null or a literal null:
The result of:
This is an important property of the operator instanceof, which makes it useful in the cast.
7. Did you know that if you call a non-static method by reference to the valuenull, the result is a NullPointerException. But you can call her on a static class method:
The output of this code:
8. You can pass null to any method that takes a reference type, for example,public void print (Object obj) can be invoked as: print (null). From the point of view of the compiler error is not here, but the behavior of this code is entirely dependent on the implementation of the method. Safe method does not throw NullPointerException in that case, and quietly exit. If the business logic allows, it is better to write secure methods.
9. You can compare null, using the operator == ("equal") and! = ("Do not care"), but not with arithmetic or logical operators (such as "more" or "less"). Unlike SQL, in Java null == nullreturn true:
The output of this code:
That's all there is to know about null in Java. If you have little experience with using simple techniques you can make your code is secure. Since null can be considered as empty or uninitialized variable, it is important to document the behavior of the method when receivingnull. Remember that any set up and initialize the variable has a default value isnull, and you can not call a method on an object or refer to his field, using null.
As we have found,
null is very important in Java. Initially it served to indicate the lack of something, such as a user, resource, and so on. N. But a year later it became clear that he brings a lot of problems. In this article we consider the basic things you should know about null pointers in Java, to minimize check null and avoid nasty NullPointerException.
1. First of all,
null - is the key word in Java, as a public, static, or final. It is case sensitive, so you can not write a Null orNULL, the compiler will not understand and will give an error:
This problem often occurs with programmers who are moving to Java from other languages, but with modern development environments this is not essential. These IDE, like Eclipse or Netbeans, correct these errors as you type code. But in times of Notepad, Vim or Emacs was a serious problem, which is time-consuming.
2. Just like any primitive type has a default value (0 in int,
false from boolean), null - the default value of any reference type, and hence for any object. If you declare a Boolean variable, it is set to false. If you declare a reference variable, it is assigned the valuenull, regardless of the scope and accessmodifiers. The only thing the compiler will warn you attempt to use an uninitialized local variable. To verify this, you can create a reference variable without initializing it, and bring it to the screen:
This is true both for static and non-static for variables. In this case, we announced
myObj as a static variable so that it can be used in static method main.
3. Contrary to popular belief,
null is neither the object nor type. It's just a special value that can be assigned to any reference type. In addition, you can also result in null to any reference type:
As you can see, cast
null to the reference type does not cause any errors during compilation or at startup. Also, when you start not NullPointerException, despite the common misconception.
4.
null can only be assigned to a variable of a reference type. Primitive types - int, double,float, or boolean - the value null assign impossible. The compiler will not allow it and will give an error:
Thus, an attempt to assign a value to
null primitive type - compile-time error, but you can assign a null type-wrapper, and then assign it a value corresponding to the primitive. The compiler will not swear, but when the code is thrown NullPointerException. This is due to automatic wrapping (autoboxing) in Java
5. Any object class-wrapper with a value of
null throw NullPointerException when deploying(unboxing). Some programmers think that the wrapper automatically assign primitive default value (0 for int, false for boolean, and so on. D.), But it is not:
If you run this code, you'll see
Exception in thread "main" java.lang.NullPointerException in the console. This often happens when dealing with HashMap with keys of type Integer. The code below will break as soon as you start:
Conclusion:
This code looks simple and understandable. We are looking for as each number is found in the array, it's a classic way to find duplicates in an array in Java. We take the number of the previous value, increment it and put back into the HashMap. We believe that
Integer will make sure to return the default value for int, but if the number is not in theHashMap, the method get () returns null, not 0. And wrap throw NullPoinerException. Imagine that the code is wrapped in a condition and insufficiently tested. Once you start it on Productions - oops!
6. The operator
instanceof returns false, when applied to a variable with a value of null or a literal null:
The result of:
This is an important property of the operator
instanceof, which makes it useful in the cast.
7. Did you know that if you call a non-static method by reference to the value
null, the result is a NullPointerException. But you can call her on a static class method:
The output of this code:
8. You can pass
null to any method that takes a reference type, for example,public void print (Object obj) can be invoked as: print (null). From the point of view of the compiler error is not here, but the behavior of this code is entirely dependent on the implementation of the method. Safe method does not throw NullPointerException in that case, and quietly exit. If the business logic allows, it is better to write secure methods.
9. You can compare
null, using the operator == ("equal") and! = ("Do not care"), but not with arithmetic or logical operators (such as "more" or "less"). Unlike SQL, in Java null == nullreturn true:
The output of this code:
That's all there is to know about
null in Java. If you have little experience with using simple techniques you can make your code is secure. Since null can be considered as empty or uninitialized variable, it is important to document the behavior of the method when receivingnull. Remember that any set up and initialize the variable has a default value isnull, and you can not call a method on an object or refer to his field, using null.
Комментариев нет:
Отправить комментарий