The Law of Flotation says that for a floating object, the weight of the object equals the weight of the liquid displaced by the object.
Submarines can rise to the surface or sink to any chosen depth by controlling their buoyancy. They do so by letting precise amounts of water or air into their ballast tanks