This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Consider the following lines of code... System.out.println(null + true) // 1 System.out.println(true + null) // 2 System.out.println(null + null) // 3 Which of the following statements are correct? a) None of the 3 lines will compile b) All the 3 line will compile and print nulltrue, truenull and nullnull respectively c) line 1 and 2 won't compile but line 3 will print nullnull d) line 3 won't compile but line 1 and 2 will print nulltrue and truenull respectively e) none of the above. And the answer is.... hmmm... don't know! The answer said option "A" was correct, but I run this code and get option "B". the explanation for this answer (option A) is:
... None of the parameters is a String so conversion to String will not happen. It will try to convert everything to an int and you will get: Incompatible type for +. Can't convert null to int.
But my compiler didn't complain at all!
P.S. Taken from JQPlus. [ July 07, 2003: Message edited by: Andres Gonzalez ] [ July 07, 2003: Message edited by: Andres Gonzalez ]
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
I compiled the above using jdk1.2.2 on solaris and got the following error T1.java:5: Reference to println is ambiguous. It is defined in void println(java.lang.String) and void println(char). System.out.println(null); ^ T1.java:6: Incompatible type for +. Can't convert null to int. System.out.println(null + true); ..... .. Which compiler are u using?? Regards Vicky
Joined: Nov 27, 2001
ooppss... Sorry Vicky, I have edited the post, changing line 2. Now it compiles.
Joined: Nov 27, 2001
One more thing:
This code compiles ok. But this one
hmmm... [ July 07, 2003: Message edited by: Andres Gonzalez ]
+ operator is overloaded only for Strings and at least one operands of + should be a String. But expressions like true+null, null+true etc. compile and run fine.
hmm... should be a String? or should be able to implicitly promote to a String. none of the operands in true+null are strings, but I guess the compiler promotes null to a String, concatenating these 2 values. Is that the reason "true + true" does not compile?