The D40 is out of production now, so stocks can be quite hard to find. Both are excellent cameras, you'll have more choice of new or secondhand lenses with the Nikon, but on the flipside the Sony has a lot more features that you might find useful.
As Munchkinn says, go and try them both and only then make your choice - don't buy a camera purely on the specification, if it doesn't feel comfortable for you to hold then you'll end up not using it....