| Author |
null, again
|
chafule razgul
Ranch Hand
Joined: Feb 09, 2002
Posts: 63
|
|
Question ID :958011966030 Consider the following lines of code... System.out.println(null + true); //1 System.out.println(true + null); //2 System.out.println(null + null); //3 1. None of the 3 lines will compile 2. Compiles and prints nulltrue, truenull and nullnull respectively 3. line 1 and 2 won't compile, but line 3 will print nullnull 4. line 3 won't compile but line1 and 2 will print nulltrue and truenull 5. None of the above. The answer says 1 but it compiles fine on my jdk1.3
|
 |
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
|
|
Well then clearly the answer is wrong then :roll: I just ran it and got this output: nulltrue truenull nullnull so I guess that means the correct answer is 2.
|
Rob
SCJP 1.4
|
 |
chafule razgul
Ranch Hand
Joined: Feb 09, 2002
Posts: 63
|
|
I just wanted to be sure, Thanks Rob
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
just my $.2 to clarify things: From JLS 15.18.1.1 String Conversion
A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression: If T is boolean, then use new Boolean(x). If T is char, then use new Character(x). If T is byte, short, or int, then use new Integer(x). If T is long, then use new Long(x). If T is float, then use new Float(x). If T is double, then use new Double(x). This reference value is then converted to type String by string conversion. Now only reference values need to be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.
[ February 28, 2002: Message edited by: Valentin Crettaz ]
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Holmes Wong
Ranch Hand
Joined: Feb 18, 2002
Posts: 163
|
|
I tried this piece of code on my JDK1.2, it gave compile errors: cannot convert null or boolean to int. I guess JDK 1.3 has a different implementation on this. Should we follow JDK1.3 as the correct answer? Well, I guess I need to update my compiler now.
|
 |
Brian Lugo
Ranch Hand
Joined: Nov 10, 2000
Posts: 165
|
|
Rob/Valentin/Others could you please confirm this for JDK 1.2 i.e. Java 2. I have JDK 1.3.1 and am not sure if I will be able to download 1.2 today or not. Is it a compile time error or not? Thanks, Brian
|
 |
Holmes Wong
Ranch Hand
Joined: Feb 18, 2002
Posts: 163
|
|
Brian, I tried it. Others please confirm since Brian does not believe me. JDK afetr 1.2 should all be called Java 2, I think. I think they should not give this kind of conflicting questions on the real exam.
Originally posted by Brian Lugo: Rob/Valentin/Others could you please confirm this for JDK 1.2 i.e. Java 2. I have JDK 1.3.1 and am not sure if I will be able to download 1.2 today or not. Is it a compile time error or not? Thanks, Brian
[ March 01, 2002: Message edited by: Holmes Wong ]
|
 |
Brian Lugo
Ranch Hand
Joined: Nov 10, 2000
Posts: 165
|
|
Sorry Holmes I did not notice your reply. I was in the POST reply mode and had that window open for a while, in the mean time you had already posted. I am taking your word ...
|
 |
 |
|
|
subject: null, again
|
|
|