Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - General Questions (Q.No. 13)
13.
Which is a valid declarations of a String?
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.
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)
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.
But you can have "null" as a string.
(1)
Purnima said:
1 decade ago
Then String s="null"; is it write?
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.
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.
Vivek said:
1 decade ago
String s="null" is perfectly fine it initializes s with null as string content.
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.
class temp
{
public static void main(String arg[])
{
String s="null";
System.out.println(s);
}
}
//output is: null.
Moin said:
9 years ago
@Swati Jain, the answer is perfectly valid.
Pranay said:
8 years ago
We have to declare a String in double quotes but it is not applicable while using null.
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).
You can have "null" as a string (Not referring to the keyword null).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers