How can I replace my old dimmer switch?
The quick answer is yes, it is ok however I'd advise to switch the wires. That is, wire nut the two white wires and connect the black wires through the dimmer switch, I'll explain.

You switch box is a hot box, any switch system has a hot box or hot lamp. If power is run directly to the lamp and only switch cables run to the switch you have a hot lamp. In this case you have a hot box, one set of those cable is live and the other set runs to the lamp outlet. On a hot box you want to bridge the neutral lines together(white) and switch the hot lines(black) that way your lamp isn't powered when change a light bulb (and the switch is turned off). 
Side note to complete: a hot lamp typically runs a black and white to the switch so you'd have one color on each side of the switch.