A submarine is a boat designed to travel at least for part of the time completely underwater.
A boat naturally floats. The air inside weighs less than the steel hull, engines and cargo. For a boat to submerge, it has to be heavier than water. It can't reduce its air space because people need to live inside it, so it must add to its internal weight by taking water into its twin-skinned hull. This is similar to sea-creatures such as a nautilus. The engines are for moving, they do not make it submerge. So a submarine is a very good example of something that works by changing its buoyancy.
The outside is designed to have as little drag in the water as possible. But if you make it the best shape in the water, it is not a stable shape when travelling on the surface. All submarines travel faster on the surface, so surface travel is an important feature.
When a submarine dives, the pressure of water on the hull increases. The best shape to resist the pressure on the hull is a ball. That shape spreads the forces evenly. Eggs are shaped to resist the weight of the bird sitting on them. But ball shapes are not good for moving about in the water. The next best shape is a cylinder. That shape is much easier to steer with. So the submarine is basically a cylinder with ball shapes at the ends.
All of those requirements explain why the submarine is such a complicated shape.
Today most submarines stay submerged for far longer than they did in the past because many of them have nuclear engines and don't need refuelling as they did in the past.