Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - Finding the output (Q.No. 2)
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); 
    } 
}
NULL
Compile Error
Code runs but no output
Runtime Exception
Answer: Option
Explanation:

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

Discussion:
5 comments Page 1 of 1.

Priyank said:   5 years ago
I think it will throw NullPointerException, which means RuntimeException.

Bagsari said:   8 years ago
Compile time error everything in string enclosed within double quotes am I right.

SKumar said:   8 years ago
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.

Jitendra said:   1 decade ago
String str =" NULL";
NULL is not a keyword.

Deep said:   1 decade ago
As per my knowledge null is not a keyword, so we can assign in any way.

Post your comments here:

Your comments will be displayed after verification.