Monday, June 4, 2018

what are the LoopHoles of null & NullPointerException

Problem statement: what do you mean by null & NullPointerException?
  • null:
  1. null is reserved literal but not keyword in java [literal mean any constant value that can be assigned to a variable]
  2. we can use null for any object reference variable
  3. if the value of reference variable null means it is not pointing to any object
  4. null is applicable to only for Object types but not for Primitive Type if we are trying to use for the primitive data types then we will get Compile Time Error
  5. null is default value for reference variable [note: make sure you should remember default values concept is applicable only for Instance & Static Variables but not for Local Variable]
  6. System.out.println(null); Compile error - reference to println is ambiguous
String s = null;
System.out.println(s); // valid
System.out.println((String)null); // valid
  • NullPointerException:
7.If we are trying to perform any Operation on the null then we will get Runtime Exception - NullPointerException
8. For any object reference r,
r == null is always false
but null == null is always true
9. for any object reference r,
r.equals(null) is always false
10. null vs NULL vs String s ="";
null & empty both are not same.
there is no concept like NULL in java
11. If an object is no longer required then we can make that object eligible for Garbage Collector by assigning null to all its reference variables.
Student s1 = new Student();
s1 = null;
Now object is eligible for GC

12. In java there are total 53 reserved word out of them 50's are keyword and 3's are reserved literal[true, false, null]

[a]-if reserved words are associated with functionality then it will keyword. e.g. if()...
[b]-if reserved words are just to represent the value then such reserved words are called reserved literals like true, false, null

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...