File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ananda s. roy
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Swamy Nathan
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have compiled the with jdk 1.3._01 version without error giving nullnull as output. :roll:
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • 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".
 
Sanyev Babu
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiles & run without error or warning both on jdk 1.3.x and 1.4.x. .
still
 
Ashok Mash
Ranch Hand
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic