The Experience Bar can be used for Enchanting. You can collect Experience by killing mobs, mining ores and sometimes from smelting iron and gold.
Once you fill up your Experience Bar you will level up. You can use the levels to enchant tools,or armor.Once you enchant something, your Experience Bar will lose levels. If you die, all your levels are lost.