• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Sybex 816 - page 129: Map.of is not overloaded with odd parameter number

 
Greenhorn
Posts: 13
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The text on Map.of says Map.of("key1", "value1", "key2"); compiles and throws runtime error, but it does not compile. Map.of is only overloaded with even parameter numbers https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html
 
Marshal
Posts: 77556
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is only overloaded to take pairs of “K”s and “V”s. It goes K, V or K, V, K, V or K, V, K, V, K, V, K, V etc.. So you can't have odd numbers of parameters, Where does it say such code compiles?
 
Jos Roseboom
Greenhorn
Posts: 13
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 129 in the section on Map.of() and Map.copyOf() is the fragment:



Right below that fragment the first line says:

This code compiles but throws an error at runtime.
 
Campbell Ritchie
Marshal
Posts: 77556
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah! Is that where it comes from? Now I understand the problem. Thank you.
 
author & internet detective
Posts: 41581
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed and added to the errata
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic