As much as we know about the human body, we know precious little about why and how hair turns grey (or white). The general idea appears to be that the follicles eventually stop producing pigment, leaving the resulting hairs white. When that happens, however, is a pretty complex thing, partly dependent on genetics, partly on environmental factors. A few known stressors that cause white hairs, other than psychoemotional stress, include serious illness, nutritional deficiencies to the point of starvation, protein deficiencies and some forms of chemotherapy (as opposed to folate depleting chemo, which turns hair a bit darker - I went from light-brown to almost black temporarily during high-load methotrexate treatment).
Caffeine is not known (beyond folk wisdom) to cause more white hairs or depigmentation. If you are worried, potential causes to look into might be a vitamin B12 deficiency, hypothyroidism and protein deficiencies, all of which could be caught easily on a simple blood test with a thyroid and vitamin panel (which is something one should get done regularly anyway). I presume you're Caucasian -- this is the age around which your hair will start going white, although initially it's very slow, and of course you didn't tell us how much hair you have. 40, though, seems to be pretty few. I suppose the only good solution is to dye your hair, if it really bothers you, but it may do more harm than good.