Java Programming - Objects and Collections - Discussion

2. 

What will be the output of the program?

public class Test 
{ 
    public static void main (String args[]) 
    {
        String str = NULL; 
        System.out.println(str); 
    } 
}

[A]. NULL
[B]. Compile Error
[C]. Code runs but no output
[D]. Runtime Exception

Answer: Option B

Explanation:

Option B is correct because to set the value of a String variable to null you must use "null" and not "NULL".


Deep said: (Dec 5, 2013)  
As per my knowledge null is not a keyword, so we can assign in any way.

Jitendra said: (Oct 3, 2014)  
String str =" NULL";
NULL is not a keyword.

Skumar said: (Apr 21, 2017)  
The null is a keyword in java and it is case sensitive. So, we can use null instead of NULL or Null.
The Following program will run successfully.

public class Test
{
public static void main (String args[])
{
String str = null;
System.out.println(str);
}
}

Output : null.

Bagsari said: (Nov 18, 2017)  
Compile time error everything in string enclosed within double quotes am I right.

Priyank said: (Nov 5, 2020)  
I think it will throw NullPointerException, which means RuntimeException.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.