• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Null Values..

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooppss... Sorry Vicky, I have edited the post, changing line 2. Now it compiles.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing:

This code compiles ok. But this one

hmmm...
[ July 07, 2003: Message edited by: Andres Gonzalez ]
 
Enthuware Software Support
Posts: 4907
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this out
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


+ 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?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler(JDK 1.1.3 on SunOS 5.6) doesn't accept any of them:
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're getting that because you're using an old version of JDK.
reply
    Bookmark Topic Watch Topic
  • New Topic