4. If $2016+3^{n}$ is a perfect square, then the positive integer $n=$ $\qquad$
4. 2 .

Obviously, $2016+3^{n}$ is an odd perfect square.
Thus, $2016+3^{n} \equiv 1(\bmod 8)$
$$
\Rightarrow 3^{n} \equiv 1(\bmod 8)
$$
$\Rightarrow n$ must be even.
Let $n=2+2 k(k \in \mathbf{N})$. Then
$$
2016+3^{n}=9\left(224+3^{2 k}\right) \text {. }
$$

Hence, $224+3^{2 k}=224+\left(3^{k}\right)^{2}$ is a perfect square.
Let $224+\left(3^{k}\right)^{2}=t^{2}\left(t \in \mathbf{Z}_{+}\right)$. Then
$$
\left(t+3^{k}\right)\left(t-3^{k}\right)=224=2^{5} \times 7,
$$

and $t+3^{k}>t-3^{k}$, both are even.
Thus, $\left(t+3^{k}, t-3^{k}\right)$
$$
=\left(2^{4} \times 7,2\right),\left(2^{3} \times 7,2^{2}\right),\left(2^{2} \times 7,2^{3}\right),\left(2^{4}, 2 \times 7\right) \text {. }
$$

Solving yields $k=0$, i.e., $n=2$.