- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Interesting facts about null in Java
There are many facts associated with null in Java. We will discuss a few of them here with examples −
The default value of any reference variable in Java is always null.
Example
public class Demo{ private static Object my_obj; public static void main(String args[]){ System.out.println("The default value of object my_obj is : " + my_obj); } }
Output
The default value of object my_obj is : null
A class named Demo defines a static object and the main function that shows the default value of this pre-defined object.
The not equal to (!=) and comparison (==) operators can be used with the null keyword.
Example
public class Demo{ public static void main(String args[]){ System.out.println("The value of null == null is : "); System.out.println(null==null); System.out.println("The value of null != null is : "); System.out.println(null!=null); } }
Output
The value of null == null is : true The value of null != null is : false
A class named Demo contains the main function that checks to see the output when the null values are compared using ‘==’ operator and when they are checked using the ‘!=’ operator.
The keyword null is case-sensitive
Example
public class Demo{ public static void main (String[] args) throws java.lang.Exception{ Object my_obj_1 = NULL; Object my_obj_2 = null; } }
Output
/Demo.java:5: error: cannot find symbol Object my_obj_1 = NULL; ^ symbol: variable NULL location: class Demo 1 error
Advertisements