  In this paper we discuss energy conservation issues related to the numerical
solution of the nonlinear wave equation. As is well known, this problem can be
cast as a Hamiltonian system that may be autonomous or not, depending on the
specific boundary conditions at hand. We relate the conservation properties of
the original problem to those of its semi-discrete version obtained by the
method of lines. Subsequently, we show that the very same properties can be
transferred to the solutions of the fully discretized problem, obtained by
using energy-conserving methods in the HBVMs (Hamiltonian Boundary Value
Methods) class. Similar arguments hold true for different types of Hamiltonian
Partial Differential Equations, e.g., the nonlinear Schr\"odinger equation.
