• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

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
  • 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: 71028
291
  • Mark post as helpful
  • send pies
  • 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
  • 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: 71028
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah! Is that where it comes from? Now I understand the problem. Thank you.
 
author & internet detective
Posts: 40283
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Confirmed and added to the errata
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic