What is the expression of electrostatic energy density in dielectric medium? I read in my book electrostatic energy density is $ \frac{1}{2} \epsilon_0 E^2$, where $\epsilon_0$ is permittivity of vacuum and $E$ Is electric field in that region. But I want to know whether it is equally valid in dielectric medium too or do we need to replace $\epsilon_0$ with $\epsilon_r×\epsilon_0$, where $\epsilon_r$ is dielectric constant of the medium?
 A: We have $\vec{\nabla}\phi=\vec{E}$ and $\vec{\nabla}\cdot\vec{D}=\rho$.
The energy change due to the addition of a tiny amount of charge (density) $d \rho$ is: $$d U_e=\displaystyle{\int_V \phi d \rho~dV=\int_V \phi \vec{\nabla}\cdot d\vec{D}~dV=\int_V (\vec{\nabla}\cdot(\phi d\vec{D})-d \vec{D}\cdot\vec{\nabla}\phi)~dV}$$
Then the total energy is: $\displaystyle{\int\ dU_e=\int_V\int_0^D(\vec{\nabla}\cdot(\phi d\vec{D})-d\vec{D}\cdot\vec{\nabla}\phi)~dV=\int_V\int_0^D\vec{\nabla}\cdot(\phi d\vec{D})-\int_V\int_0^Dd\vec{D}\cdot\vec{\nabla}\phi~dV}$.
The first term vanishes when we integrate over the entire space (as it does in a non-dielectric media) and the only thing left is: $\displaystyle{\int_V\int_0^Dd\vec{D}\cdot\vec{E}~dV}$. Now in a linear medium: $\vec{D}=\epsilon_0\epsilon_r\vec{E}$ so $d\vec{D}=\epsilon_0\epsilon_rd\vec{E}$.
Back at the energy: $U_e=\displaystyle{\epsilon_0\epsilon_r\int_V\int_0^Ed\vec{E}\cdot\vec{E}~dV=\epsilon_0\epsilon_r\int_V\int_0^Ed\vec{E}\cdot\vec{E}~dV=\dfrac{1}{2}\epsilon_0\epsilon_r\int_V\vec{E}\cdot\vec{E}~dV}$
So indeed, you're right the density is: $\dfrac{1}{2}\epsilon_0\epsilon_rE^2$.
Note here that $\rho$ is the density of free charges.
