MESSAGE
DATE | 2016-05-16 |
FROM | ruben safir
|
SUBJECT | Subject: [Hangout-NYLXS] Fwd: Re: hashtables syntax
|
-------- Forwarded Message -------- Subject: Re: hashtables syntax Date: Sun, 15 May 2016 07:44:16 -0400 From: Eric Sosman Organization: A noiseless patient Spider Newsgroups: comp.lang.java.programmer References:
On 5/15/2016 2:31 AM, Popping mad wrote: > > This whole syntax is new and strange to me, seemingly illogical > > private static HashMap functions; // built-in > function types > > static { // initialize function types > functions = new HashMap<>(); > functions.put("floor", new Type[]{Type.FLOAT, Type.INT}); > functions.put("sqrt", new Type[]{Type.FLOAT, Type.FLOAT}); > functions.put("log", new Type[]{Type.FLOAT, Type.FLOAT, > Type.FLOAT}); > functions.put("random", new Type[]{Type.FLOAT}); > functions.put("parseInt", new Type[]{Type.STRING, Type.INT}); > functions.put("parseFloat", new Type[]{Type.STRING, Type.FLOAT}); > functions.put("readLine", new Type[]{Type.STRING}); > functions.put("showInt", new Type[]{Type.INT, Type.STRING}); > functions.put("showFloat", new Type[]{Type.FLOAT, Type.STRING}); > } > > > If we are creating a private static Hashmap with template elements of a > String and and array of Types, then why do I have to call > > functions = new HashMap<>();
You need to create the HashMap before you can start filling it. You can't put the animals into the Ark before the Ark is built.
> and why nothing in the template. And what does static do in this case?
By "template" I guess you mean the <> angle braces? That's where the type parameters would go, just as in the declaration. In fact, it would be perfectly legal to repeat the parameters -- but since Java can figure them out for itself ("Hmmm: They're probably the same types as I already saw when `functions' was first declared!"), why bother?
As for `static', I'm not sure which of the two appearances confuses you. The first `private static ... functions;' means that `functions' belongs to the class as a whole, not to any instance of the class. If `static' weren't there, each instance of the class would have its very own `functions' map. The second appearance `static { // initialize ...' means that this code belongs to the class as a whole and not to any particular instance; it will execute when the class itself is being set up -- and thus, will populate `functions' on behalf of the class.
-- esosman-at-comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM _______________________________________________ hangout mailing list hangout-at-nylxs.com http://www.nylxs.com/
|
|