Originally posted by raja pratap:
Which of the following are valid statements and why?
Can anyone help me out??
[1]System.out.println(true);
[2]System.out.println(null+null);
[3]System.out.println(true+null);
[4]System.out.println(true+8);
I don't really consider this a very good question. I can say that because I'm the author of the question <grin>. I was orginally trying to
test whether or not you knew that System.out.println(null) would work. The reason that I now consider this a poor question is because different versions of the JDK give different results.
When I originally tested this in VisualAge for Java (where I wrote my mock) the answers are as I indicated (the correct answers in VaJava were 1,2,3.) I tried to also test everything outside VaJava but apparently I missed this one. <dang>
I get interesting results depending on the version of the JDK I use (all of these on WinNT):
J D K 1.1.8 [correct answers would be 1]
NullTest.java:4: Reference to println is ambiguous. It is defined in void println (java.lang.String) and void println(char[]).
System.out.println(null);
^
NullTest.java:5: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+null);
^
NullTest.java:5: Incompatible type for +. Can't convert null to int.
System.out.println(true+null);
^
NullTest.java:6: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+8);
J D K 1.3 (build 1.3beta-O) [correct answers would be 1, 3]
NullTest.java:4: reference to println is ambiguous, both method println(char[])
in java.io.PrintStream and method println(java.lang.String) in java.io.PrintStre
am match
System.out.println(null);
^
NullTest.java:6: cannot resolve symbol
symbol : method + (boolean,int)
location: class NullTest
System.out.println(true+8);
J D K 1.2 ((Classic VM (build JDK-1.2-V, native threads)) [correct answers would be 1]
NullTest.java:4: Reference to println is ambiguous. It is defined in void printl
n(java.lang.String) and void println(char[]).
System.out.println(null);
^
NullTest.java:5: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+null);
^
NullTest.java:5: Incompatible type for +. Can't convert null to int.
System.out.println(true+null);
^
NullTest.java:6: Incompatible type for +. Can't convert boolean to int.
System.out.println(true+8);
Clearly I should have tested this better, and of course System.out.println((Object)null); will work fine too.