I'm reading Chapter 2 in Introduction to Stochastic Processes by Erhan Çinlar and I cannot understand their proof for Theorem 1.9. Before giving the theorem, they give the definition
Def. 1.2: The expected value of a discrete random variable $X$ taking values in the set $E \subset \mathbb{R}_+$ is
$$E[X] = \sum_{a\in E}aP\{X=a\}$$
Theorem 1.9 states that for any non-negative random variable $X$,
$$E[X] = \int_0^\infty P\{X > t\}dt$$
Proof: First suppose $X$ is discrete with values in $E$. Then using Definition (1.2) and changing the order of the summation and integration, we get
$$E[X]=\sum_{a\in E}aP\{X=a\}$$ $$=\sum_{a\in E} \int_0^adtP\{X=a\}$$ $$=\int_0^\infty dt \sum_{a>t}P\{X=a\} = \int_0^\infty P\{X>t\}dt$$
I just can't understand the third line. Why are we able to switch the summation sign and the integral sign? Why is the third line equivalent to the second line?