0% found this document useful (0 votes)
3 views3 pages

Part 12

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Part 12

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java null reserved word

In Java, null is a reserved word for literal values. It seems like a keyword, but actually, it is a literal
similar to true and false.

Points to remember

o It is case-sensi ve.

o It is a value of the reference variable.

o The access to a null reference generates a NullPointerExcep on.

o It is not allowed to pass null as a value to call the methods that contain any primi ve data
type.

Examples of Java null reserved word

AD

Example 1

Let's see a simple example to display the default value of the reference variable.

1. public class NullExample1 {

2. sta c NullExample1 obj;

3. public sta c void main(String[] args) {

4.

5. System.out.println(obj);

6. }

7.

8. }

Output:

null

Example 2

Let's see an example to determine whether we can pass null to object reference variable.

1. public class NullExample2 {

2.

3. public sta c void main(String[] args) {

4. NullExample2 obj = null;

5. System.out.println(obj);

6. }

7.
8. }

Output:

null

Example 3

Let's see an example to display the default value of String.

1. public class NullExample3 {

2.

3. String str;

4. public sta c void main(String[] args) {

5. NullExample3 obj=new NullExample3();

6. System.out.println(obj.str);

7. }

8.

9. }

10. </pre></div>

11. <p><strong>Output:</strong></p>

12. <div class="codeblock3"><pre>

13. null

14. </pre></div>

15. <h2 class="h3">Example 4</h2>

16. <p>Let's see an example to return null from a method.</p>

17. <div class="codeblock"><textarea name="code" class="java">

18. public class NullExample4 {

19. String display()

20. {

21. return null;

22. }

23. public sta c void main(String[] args) {

24. NullExample4 obj=new NullExample4();

25. System.out.println(obj.display());

26. }
27. }

Output:

null

AD

Example 5

Let's see an example to provide null to the String variable.

1. public class NullExample5 {

2. sta c String str=null;

3. public sta c void main(String[] args) {

4. if(str==null)

5. {

6. System.out.println("value is null");

7. }

8. else

9. {

10. System.out.println("value is not null");

11. }

12. }

13. }

Output:

value is null

You might also like