Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 13)
13.
Which is a valid declarations of a String?
String s1 = null;
String s2 = 'null';
String s3 = (String) 'abc';
String s4 = (String) '\ufeed';
Answer: Option
Explanation:

Option A sets the String reference to null.

Option B is wrong because null cannot be in single quotes.

Option C is wrong because there are multiple characters between the single quotes ('abc').

Option D is wrong because you can't cast a char (primitive) to a String (object).

Discussion:
11 comments Page 1 of 2.

Satish Kanakala said:   3 years ago
The mentioned answer

String S = null is perfectly valid because it is the default value of the string. As conclusion, if we initialised any explicit value in double quotes like "null" or any "xxx". It will give results as mentioned in double-quotes.

Thank you

Abdul Isaac Koroma said:   3 years ago
It is not clear since String s="null" work perfectly fine.

But you can have "null" as a string.
(1)

Jay said:   5 years ago
It is not clear since String s="null" works perfectly fine.

You can have "null" as a string (Not referring to the keyword null).

Pranay said:   8 years ago
We have to declare a String in double quotes but it is not applicable while using null.

Moin said:   9 years ago
@Swati Jain, the answer is perfectly valid.

Swati Jain said:   1 decade ago
@Ganesh : null is written when the value is unknown or yet to be assigned. Here by writing null in double quotes you have not written the null as used but you have rather defined it as a string containing the value equals to null. This is no more a string with no value now.
(1)

Ganesh said:   1 decade ago
/*String s="null" is correct "null" is consider as String and null is not a keyword so it's correct. if you have any doubt run this example*/

class temp
{

public static void main(String arg[])
{
String s="null";
System.out.println(s);

}

}

//output is: null.

Vivek said:   1 decade ago
String s="null" is perfectly fine it initializes s with null as string content.

MAHITEJA YENUMULA said:   1 decade ago
Exactly what garima said id right the reason behind is null cannot be given inside single or double quotes.

Garima said:   1 decade ago
No String s="null" is not right you don't give null in double quotes. However if you are initializing it with some other literal say String s="hello", double quotes are required.


Post your comments here:

Your comments will be displayed after verification.