• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why System.out.println(null+null) gives nullnull ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while using

gives compile time error complaining about method ambiguity but

outputs nullnull.
I know that + operator converts the operands to string if any one of the operand is a string, but i am in the above case.
Thanks in advance.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both ur statements wont compile but this will

System.out.println(""+null+null);

I suspect it has something to do with StringBuffers and that appending a null gives a appending of a "null" text literal
 
ananda s. roy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have compiled the with jdk 1.3._01 version without error giving nullnull as output. :roll:
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null objects are treated as a String "null" by output mechanisms producing Strings.
null + null will therefore generate "null" + "null" = "nullnull".
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well for me null+null is giving compile time error.

The operator + is undefined for the argument type(s) null, null
 
Swamy Nathan
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont have JDK1.3 but I checked up with 1.4. It does not compile.
If there is indeed a discrepancy the importrant question is what is the correct behaviour from certification point of view?
Ananda did you type in nul+null or null+null?

Jeroen Wenting's theory does not feel right. Any output from System.out should only happen after null+null (which does not compile). But one never knows. Did it compile for anyone else?
[ May 20, 2004: Message edited by: Swamy Nathan ]
 
ananda s. roy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiles & run without error or warning both on jdk 1.3.x and 1.4.x. .
still
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, System.out.println(null+null) prints "nullnull" in jdk 1.3.1_06 for me. I don't know why though! Must be one of those things they fixed later!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code prints nullnull:



If you read the two snippets of code from the j2sdk1.4.2_04 source files, then you will get a better idea what happens. What will be executed is something very like:


From String.java:


From StringBuffer.java:
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, whit a string that work but whit a primitive doesnt,if we try this, we get a complier error:
System.out.println(null+ null+1);


Congratulation Barry in you post number 2500!


 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "overloaded" string concatentation operator is defined to work when one operand is a reference to a String and the other is either a primitive, a reference to a String, or a reference to an object. The object reference can be null, in which case the string "null" will be used.

So "" + null, and null + "" are both OK, and result in a String "null". null + null should not work because not one of the operands is a reference to a String. The compiler can now detect this case and gives a compilation error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic