Cartesian diver

This straw diver contains an air bubble sealed inside. Without squeezing the bottle the overall density of the diver is slightly lower than that of the water, so it floats. When you squeeze the sides of the bottle you increase the pressure on the air bubble, compressing it into a smaller space. This decrease in volume of the bubble causes an increase in the overall density of the straw diver. When it becomes greater than that of the surrounding water it sinks. Releasing the pressure (by releasing the bottle from your grip) allows the air bubble to expand back to its normal size, and the straw diver floats again.

Human divers use weights to increase their density when diving, but submarines have tanks of compressed air on board to help control their buoyancy. Surrounding the submarine are a number of ballast tanks which, when filled with water, increase the overall density of the submarine and it dives. But when the submarine needs to rise to the surface, the water in these large ballast tanks is replaced with air from the compressed air tanks.