I am given this additional formula for E[X] when X does not take on negative values.
I cannot figure out why the second formula is correct. I see why this is correct when X < k (which I assume means that X takes on no value greater than k). I cannot figure out why this is true when k < X

Integrate by parts. $\int_0^k(1-F(x))dx=x(1-F(x)]_0^k+\int_0^kxF'(x)dx=k(1-F(k))+\int_0^kxf(x)dx$