aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null+null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null+null" Watch "null+null" New topic
Author

null+null

Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
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

Joined: Aug 25, 2003
Posts: 123
i am using jdk1.3 and getting same answer as yours
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
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

Joined: Jun 25, 2003
Posts: 46
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: May 05, 2000
Posts: 13974
The toString method isn't invoked by the + it is invoked in the println method.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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

Joined: Jul 30, 2003
Posts: 804
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Aug 25, 2003
Posts: 504
Thanks Thomas, then we shouldn't expect questions on bugs I hope.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null+null