This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why System.out.println(null+null) gives nullnull ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why System.out.println(null+null) gives nullnull ?" Watch "why System.out.println(null+null) gives nullnull ?" New topic
Author

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

ananda s. roy
Greenhorn

Joined: May 13, 2004
Posts: 12
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.


SCJP 1.4
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
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

Joined: May 13, 2004
Posts: 12
i have compiled the with jdk 1.3._01 version without error giving nullnull as output. :roll:
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
null objects are treated as a String "null" by output mechanisms producing Strings.
null + null will therefore generate "null" + "null" = "nullnull".


42
Sanyev Babu
Ranch Hand

Joined: Dec 18, 2003
Posts: 132
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

Joined: May 16, 2004
Posts: 187
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

Joined: May 13, 2004
Posts: 12
compiles & run without error or warning both on jdk 1.3.x and 1.4.x. .
still
Ashok Mash
Ranch Hand

Joined: Oct 13, 2000
Posts: 1936
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!


[ flickr ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
wood burning stoves
 
subject: why System.out.println(null+null) gives nullnull ?