File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null+null

 
Rishi Wright
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi 'all
In all mock exams the following gives an compile time error
public class Test {
public static void main(String args[ ] ) {
System.out.println(null + true); //1
System.out.println(true + null); //2
System.out.println(null + null); //3
}
}
but when Iam trying it ,this comiples fine(iam using Jdk1.4) and gives the output as nulltrue,truenull and nullnull.
what should be the answer if such question comes in real exam??
thnak u
rishi
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using jdk1.3 and getting same answer as yours
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled and tested with 1.4.1. No problem, and had the same results. Which mock exam did you see this in? Could this have been a problem in 1.2? Maybe there's a discussion thread in the products topic area?
Regards,
Jeff
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Binary operatoe + is overloaded to perform concatenation operation.
when one of the operand is string other is implicitly converted to string.
Numeric operand is converted to string object with string representation of value.
For non-string object tostring() method is used to convert it to string.
Values like true,false and null are implicitly represented by string representation of the value.Hence the answer.
 
Rishi Wright
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently I have seen this question in Technopark mock exam ..and mainly I came across this in javaranch discussions by maha anna..
I dont have jdk 1.2 so I never tried in that..

rishi
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This doesn't compile with jdk 1.4.2, and I don't see why it should. + operator is overloaded for String concatenation, but I don't see any String parts in all the three examples. + operator also works fine on numeric expressions with promotions or on mixed expressions like (true+"") or (4+5+""). You are already in trouble having something like
because + is stupid and doesn't invoke toString() method like that.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The toString method isn't invoked by the + it is invoked in the println method.
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I just meant that + operator won't accept the object reference without having a String operand somewhere in the expression. Please correct me if I'm wrong.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should compile, according to my reading of the JLS:
Concatenation (and toString() ) are performed by the println() method. When concatenating String objects with other objects, the value being concatenated is evaluated as to whether it's a value, a primitive, or an object.
If a reference value is null, it is converted to the String "null". That's one String in this example. Also, if the value is boolean, it is treated as though constructed using new Boolean( value ).toString(). There's the "true" part in this example.
See JLS, Third Edition, 15.18.1
I don't know why 1.4.2 would not let it compile when 1.4.1 does. Can you copy and post your source code?
Regards,
Jeff
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
This is a bug and has been now fixed. link.
You can only use + (for Strings) when one of the operands is a String. In the above case there wasn't any.
Probably you won't get this on the exam, if you do then select the code won't compile.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Bosch:
If a reference value is null, it is converted to the String "null". That's one String in this example.

Not quite. Read that first statement again. It says "if a reference value is null". null is not a reference value. The statement above is referring to this:
Object obj = null
System.out.println(obj); // will print "null"
obj is a reference value set to null.
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me ask you a question Thomas:

I wonder how to explain rather an unexpected output that to some degree ruins the concept of toString() method invocation.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vad Fogel:
I wonder how to explain rather an unexpected output that to some degree ruins the concept of toString() method invocation.

There is a bug logged about this:
http://developer.java.sun.com/developer/bugParade/bugs/4339222.html
As the response says:
Any toString implementation that returns null has violated the toString spec. Violating the general contract of a method can result in arbitrary behavior.
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Thomas, then we shouldn't expect questions on bugs I hope.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic