Saturday, 31 August 2013

Assignment Operation Java

Assignment Operation Java

class string1
{
static public void main(String[] ar)
{
String s1 = "test";
String s2 = new String();
if(s2=null) //Line 0
{
System.out.println(s1);
System.out.println(s2.length()); //Line 1
}
}
}
What i expected to happen was
a) s2 will be set to null because its an assignment operation as I am not
using ==
b) And I will get a NPE at Line 1 during Runtime
and instead I got the following output as shown below.
Output is
if(s2=null)
^
required: boolean
found: String
1 error
Can someone explain why I am getting compile error ?

No comments:

Post a Comment