When a planet transits its star (see here), and we’re able to determine it’s mass through some other means, typically Doppler spectroscopy (see here and here), then we can derive its density rather easily.
Where is the volume of the planet, equivalent to .
Planets of similar densities are not necessarily the same in bulk composition. With the bulk composition being held constant, the radius will increase with mass in a non-linear way such that the increase in the radius slows, while the density increases. After enough mass is built up, the trend reverses, with the gravity compressing the planet more as additional mass is added.
For planets made purely of one substance (half-seriously referred to as “mathematician’s planets”), one can plot their mass and radius on a diagram and see that we actually can use a planet’s observed mass and radius to infer something about its composition, assuming iron, rock, water and gas are the dominant things out of which a planet will be comprised. This seems to be a reasonable assumption based on what we know of how planets form and the observed composition of planet-forming disks.
Plotting the Solar System planets (minus Mercury and Venus) on such a diagram, we see that the radius of Earth and Mars are consistent with being almost entirely rock, with Earth being obviously too dense to be entirely made of rock, requiring an iron component. Uranus and Neptune are slightly too large for their mass to be explained by a composition of 100% water. So a (small, by fraction of mass) gaseous component must exist. Jupiter and Saturn are far too large for their mass to be explained by being mostly water in composition, so it’s clear that they are mostly made of gas.
These things are, for the most part, able to be determined through direct study of the planet themselves, but this is not easy to do for extrasolar planets. For the transiting planets, we’re afforded a radius and a mass and left to interpolate a composition based on it. A complication arises in that there are not unique solutions for the composition of a planet for some values of the radius and mass. A 100% gas planet will stick out as being purely gas, while a 100% iron planet will clearly be an enormous cannonball. But planets can have layers of all three and this can create degeneracies in models of their interior. An small ball of iron with a large layer of water and gas will have a similar radius to a large rocky planet with a small layer of gas. As a real example (and as of the time of this writing), it is not clear if GJ 1214 b is a sort of “mini-Neptune,” with a larger core and a lot of water, or a sort of “micro Jovian,” with a small core and a large gaseous envelope. Even Uranus and Neptune show that there’s some ambiguity here. The two are believed to have solid cores under water mantles, but the mass of the rock and/or iron is hard to disentangle from the mass of the water mantle.
So it is clear that different compositions can be thrown together to blur the classification of planets into neat categories of terrestrial planets, ice giants, and gas giants. Indeed, they do not separate out very well in a mass-radius diagram.