| Author |
Null Values..
|
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
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
|
 |
Vicky Nag
Ranch Hand
Joined: Jul 02, 2003
Posts: 40
|
|
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
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
|
ooppss... Sorry Vicky, I have edited the post, changing line 2. Now it compiles.
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
One more thing: This code compiles ok. But this one hmmm... [ July 07, 2003: Message edited by: Andres Gonzalez ]
|
 |
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2912
|
|
Check this out
|
Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
+ 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?
|
 |
Darren McLeod
Greenhorn
Joined: Jul 07, 2003
Posts: 19
|
|
My compiler(JDK 1.1.3 on SunOS 5.6) doesn't accept any of them:
|
SCJP, SCBCD
|
 |
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
|
I think you're getting that because you're using an old version of JDK.
|
 |
 |
|
|
subject: Null Values..
|
|
|