• Post Reply Bookmark Topic Watch Topic
  • New Topic

String ambiguous constructor ?!  RSS feed

 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code above compiles just fine. The code below however gives a compile error on line 2:



Why is line 2 above giving "The constructor String(String) is ambiguous" compile error ???
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because the compiler can not determine which constructor of String class you want to call.

There are five candidates thet could be used with null.
- String(byte[] bytes)
- String(char[] value)
- String(String original)
- String(StringBuffer buffer)
- String(StringBuilder builder)

You can use a cast on null to determine the constructor to call, like:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!