Why is a gap left between the ends of two railway lines in a railway track?

It is done to accommodate the linear expansion of railway line during summer. If the gap is not left, in summer, the lines will bend causing a threat of derailment.