Expectation of random variable. Somewhere I found the definition of expectation of random variable as $$E[X]=\int_{0}^{\infty}(1-F(x))\,dx-\int_{-\infty}^0F(x)\,dx$$
But the definition I know is $$E[X]=\int_{-\infty}^{\infty}xf(x)\,dx$$

My Question is  why $$\int_{-\infty}^{\infty}xf(x)\,dx=\int_{0}^{\infty}(1-F(x))\,dx-\int_{-\infty}^0F(x)\,dx$$

 A: It is not a definition — at best an identity, or a proposition, but I know of nowhere where the expectation would be introduced as the first expression you gave.
Assuming all is well (the expectation is well-defined, for a start, and $X$ admits a density), let us write $a=\int_{0}^\infty (1-F(x))dx - \int_{-\infty}^0 F(x)dx$:
$$\begin{align}
a &= \int_{0}^\infty (1-F(x))dx - \int_{-\infty}^0 F(x)dx\\
&= \int_{0}^\infty (1-\int_{-\infty}^uf(u)du)dx - \int_{-\infty}^0 \int_{-\infty}^u f(u)dudx\\
&= \int_{0}^\infty (1-\int_{-\infty}^\infty \mathbb{1}_{(-\infty,x]}(u)f(u)du)dx - \int_{-\infty}^0 \int_{-\infty}^\infty \mathbb{1}_{(-\infty,x]}(u)f(u)dudx\\
&= \int_{0}^\infty \int_{-\infty}^\infty (f(u)-\mathbb{1}_{(-\infty,x]}(u)f(u)du)dx - \int_{-\infty}^0 \int_{-\infty}^\infty \mathbb{1}_{(-\infty,x]}(u)f(u)dudx\\
&= \int_{0}^\infty \int_{-\infty}^\infty \mathbb{1}_{(-\infty,x]}(u)f(u)dudx - \int_{-\infty}^0 \int_{-\infty}^\infty \mathbb{1}_{(-\infty,x]}(u)f(u)dudx\\
&= \int_{-\infty}^\infty \int_{0}^\infty\mathbb{1}_{(x,\infty)}(u)f(u)dudx + \int_{-\infty}^\infty \int_{-\infty}^0 -\mathbb{1}_{(-\infty,x]}(u)f(u)dudx\\
&= \int_{-\infty}^\infty du\ f(u) \left(\int_{0}^\infty\mathbb{1}_{(x,\infty)}(u)dx - \int_{-\infty}^0\mathbb{1}_{(-\infty,x]}(u)dx\right)\\
\end{align}$$
Now, it remains to observe that for any $u\in\mathbb{R}$,
$$\begin{align}
\int_{0}^\infty\mathbb{1}_{(x,\infty)}(u)dx - \int_{-\infty}^0\mathbb{1}_{(-\infty,x]}(u)dx
&=
\int_{0}^\infty\mathbb{1}_{(0,u)}(x)dx - \int_{-\infty}^0\mathbb{1}_{[u,0]}(x)dx \\
&= \begin{cases}
\int_{0}^\infty\mathbb{1}_{(0,u)}(x)dx = u& \text{ if } u> 0 \\
- \int_{-\infty}^0\mathbb{1}_{[u,0]}(x)dx = u& \text{ if } u \leq 0 \\
\end{cases} \\
&= u
\end{align}$$
so overall
$$\begin{align}
\int_{0}^\infty (1-F(x))dx - \int_{-\infty}^0 F(x)dx
&= \int_{-\infty}^\infty du\ u f(u) = \mathbb{E}[X].
\end{align}$$
