Whether a submarine floats or sinks in water depends on its overall density compared to the density of water.
The principle at play here is known as buoyancy, which is governed by Archimedes' principle. According to this principle, an object immersed in a fluid experiences an upward buoyant force equal to the weight of the fluid it displaces. If the buoyant force is greater than or equal to the weight of the object, it will float; if the buoyant force is less than the weight, the object will sink.
In the case of a submarine, it can control its buoyancy by adjusting the amount of water or air inside its ballast tanks. When a submarine wants to float, it fills its ballast tanks with air, which increases its overall buoyancy and makes it less dense than the water. This allows the submarine to displace a volume of water greater than its own weight, resulting in a net upward buoyant force that keeps it afloat.
Conversely, when a submarine wants to submerge, it fills its ballast tanks with water, displacing the air inside. This increases the submarine's density, making it heavier than the water and causing it to sink. To control its depth while submerged, the submarine adjusts the amount of water in its ballast tanks, enabling it to increase or decrease its buoyancy.
By manipulating its buoyancy through the management of ballast tanks and the distribution of water and air, a submarine can effectively control whether it floats on the water's surface or submerges beneath it.