# Why does the identity $\mathbb{E}(X) = \mathbb{E}\left(\int \mathbb{1}_{u \leq X}du\right)$ hold?

I'm reading on Hoeffding's covariance identity, the proof of which is neatly covered here, or, in a similar manner, in this MSE post, but I can't seem to fully understand the trick/property used there.

I.e., assume $(X_1, Y_1)$ and $(X_2, Y_2)$ are two independent vectors with identical distribution. The key point in the proof is to note that we can write

$$\mathbb{E}[(X_1 - X_2) (Y_1 - Y_2)]$$ as

$$\mathbb{E}\left(\iint_{\mathbb{R}\times\mathbb{R}} [\mathbb{1}_{u\leq X_1} - \mathbb{1}_{u \leq X_2}] \cdot [\mathbb{1}_{v\leq Y_1} - \mathbb{1}_{v \leq Y_2}]\,du\,dv \right)$$

Why does this hold?

What underlies the equality $\mathbb E(X) = \mathbb E(\int \mathbb 1_{u\le X}\,du)$ is, intuitively, the way one thinks of the Lebesgue integral as coming from partitioning the $y$-axis, whereas the Riemann integral comes from partitioning the $x$-axis.
Think of a reasonable function $f(x)$ (say continuous, but that's not necessary, and nonnegative to be concrete). We think of $\int_{-\infty}^\infty f(x)\,dx$ as the area under the curve $y=f(x)$. Now write this as an iterated integral and then change the order of integration: $$\int_{-\infty}^\infty f(x)\,dx = \int_{-\infty}^\infty\int_0^{f(x)} 1\,dy\,dx = \int_0^\infty \mu(\{x: f(x)\ge y\}\,dy.$$ The $x$ cross-section at height $y$ is precisely the set of points $x$ where $f(x)\ge y$. Here $\mu(E)$ is the (Lebesgue) measure of $E\subset\Bbb R$.
For any point $\omega$ in the sample space, we have $$\int_{\mathbb R \times \mathbb R} [1_{u\leq X_1(\omega) } - 1_{u \leq X_2(\omega)}][1_{v \leq Y_1(\omega)} - 1_{v \leq Y_2(\omega)}] \ du dv =(X_1(\omega) - X_2(\omega))(Y_1(\omega)-Y_2(\omega))$$
[For example, if $X_1(\omega) > X_2(\omega)$ and $Y_1(\omega) > Y_2(\omega)$, then \begin{align} \int_{\mathbb R \times \mathbb R} [1_{u\leq X_1(\omega) } - 1_{u \leq X_2(\omega)}][1_{v \leq Y_1(\omega)} - 1_{v \leq Y_2(\omega)}] \ du dv &=\int_{\mathbb R \times \mathbb R}1_{X_2(\omega) < u \leq X_1(\omega)}.1_{Y_2(\omega) < v \leq Y_1(\omega)} \ dudv \\ &= \int_{X_2(\omega)}^{X_1(\omega)}\int_{Y_2(\omega)}^{Y_1(\omega)} 1 \ dudv \\ &=(X_1(\omega) - X_2(\omega))(Y_1(\omega)-Y_2(\omega))\end{align} It's not to hard to find analogous arguments for the cases where $X_1(\omega) \leq X_2(\omega)$ or $Y_1(\omega) \leq Y_2(\omega)$.]