Most matter expands when heated and contracts when cooled, a principle called thermal expansion. The average kinetic energy of the particles increases when matter is heated and this increase in motion increases the average distance between its atoms. It is important to note that water does not follow the rule of thermal expansion. Water expands when it freezes because the crystalline structure of ice takes up more space than liquid water.
Have you ever noticed when you put a warm bottle of soda into your refrigerator, come back in a couple of hours, it's buckled in and it's lost some of its volume? Well you'll notice that especially if there's a lot of air in that bottle. What you're seeing is the result of thermal expansion or in this case thermal contraction and what that basically says is thermal expansion as an object is heated, molecules move faster and they gain kinetic energy and they bounce further away from each other and that results in an expansion of that object. Conversely when an object is cooled, the object loses kinetic energy, those molecules are moving slower and they're going to pack together a little bit closer so we see contraction when an object is cooled. You may have noticed if you have trouble getting the rid of of a pickle jar for example, you can get it up more easily by running it under hot water for several seconds that how water is going to heat up the metal rid and it's going to expand and then you can more easily get it off.
There is one exception, we say most matter expands when heated and contracts when cooled and that one exception is water and specifically when water approaches it's freezing point, as water is cooled, it will continue to contract and contract but when it reaches it's freezing point, often it's going to expand and this is why we see ice floating ice has a lower density than does the water surrounding it pushing it up, why is that? Well it turns out that water in its liquid form sticks to itself things called hydrogen bonds cause it have a fairly high density and when it freezes it goes into a crystalline formation where molecules are actually further apart so this, this property of water that when it freezes it expands allows life to exist on earth. Our oceans would freeze in the winter when all that ice sunk to the bottom and continue to freeze but fortunately floating ice provides sort of a greenhouse effect that prevents our oceans from freezing solid so an important quality of water the fact that it contradicts thermal expansion at that point of freezing, but for the most part all other matter expands when heated and contracts when cooled.