My sister has a D40 and I have an A300 (A200 + Live View + tilting LCD and maybe some other extras, I'm not too sure, my Grandma bought it for herself, didn't like such a big camera and then gave it to me).
I'd messed about with my sister's D40, played with settings and really loved it. When I tried the A300 for the first time, the main thing I noticed was it was a LOT heavier than the D40, then it took a little time getting used to the interface, but is good.
This is a photo I took with the D40 (not using the kit lens, it was a Telephoto lens that came in the package my sister bought):
This is a photo taken with my A300, using a Tamron 70-200:
They aren't much of a comparison on the bodies, but much more on the lenses, and the idiot behind said camera
I really like the Sony, and although it is technically a Sony, it's basically Minolta, so there are plenty of cheap good lenses out there second hand, and Sony seem to be making good (but expensive) lenses as well.
The main thing to do is see how the bodies feel, do a little research into the lenses and what you want to take photos of, then who's 'System' you want to buy into. Lenses will last you years, but you may end up replacing the body every 3 or 5 years. You don't want to end up losing money on the lenses you buy if you find you need to change to a different company.
The one downer on the D40 is its need for AF-S lenses (to have Autofocus). For instance, on amazon.co.uk, a regular 50mm 1.4 af lens is £229, the one which will autofocus with a D40 is £311. So that may be a reason to go for a higher Nikon than the D40.