• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange behavior

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody provide an explanation to this please..
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic