my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange behavior 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 "Strange behavior" Watch "Strange behavior" New topic
Author

Strange behavior

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Moving it to a seperate topic.

Having said that here is a strange behavior



s3 will have string "null"
And s3.toUpperCase() will result in "NULL"
So If s1 and s2 were converted to StringBuffer or StringBuilder then how come StringBuilder s5 = new StringBuilder(null); and StringBuffer s = new StringBuffer(null); throw NullPointerException ?

Second Question is
If StringBuilder s5 = new StringBuilder(null); and StringBuffer s = new StringBuffer(null); do not throw "Compiler Error" Why does
String s66 = new String(null); throw a compiler error "The constructor String(String) is ambiguous"
This example was tested with Eclipse and With Java 1.5
Thanks
Deepak


SCJP, SCWCD, SCBCD
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
s3 is null. System.out is a java.io.PrintStream. If you look at the source code for println(Object) you see that it calls String.valueOf(Object) which converts any null value into the String "null".

When you say "new StringBuffer(null)" you call the StringBuffer(String) constructor. In the source code you can see that the first thing this constructor does is call the length() method on the string that is passed in. This will obviously result in a NullPointerException if the argument is null.

In the case of StringBuilder, the same thing happens.

The second question. String's constructor is overloaded. That means that the compiler has to infer by the number and types of arguments that you pass to the constructor, which one is intented. However new String(null) could mean
new String((String)null), new String((StringBuilder)null) or new String((StringBuffer)null), i.e., the call is ambiguous.


SCJP 1.4, SCWCD 1.4
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
No its not correct

If s3 was null. then s3 = s3.toUpperCase(); should have resulted in exception which it did not. After reading Java Specs , it says if one of the operand of "+" is null then null is converted to String "null" and used.
I hope you agree to this.
Thanks
Deepak
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
Can somebody provide an explanation to this please..


SCJP 5.0, SCWCD in progress
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Originally posted by Deepak Jain:
If s3 was null. then s3 = s3.toUpperCase(); should have resulted in exception which it did not. After reading Java Specs , it says if one of the operand of "+" is null then null is converted to String "null" and used.
I hope you agree to this.
Thanks
Deepak


String s3 = s1 + s2;

String addition is actually syntatic sugar -- the compiler converts the operations to a StringBuffer/StringBuilder instantiation, followed by calls to the append() method, and finally, by a call to toString() to get the string.

The append() method will convert all null references to "null" strings during the append. So after the addition of two nulls, you should have s3 equal to "nullnull".

s3 = s3.toUpperCase();

The toUpperCase() method will return a new string, in upper case. So s3 should now be referenced to a string of value "NULLNULL".


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
Actually, I overlooked the fact that s3 should have been null according to common sense. I tried it out and I get "NULLNULL" indeed. Thanks! Very interesting. This is one to remember for the exam!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange behavior