First, go here:
Click on JDK download box. This takes you to a new page with all the downloads on it.
Download the correct installation for your machine. For this tutorial, we will use the one that says Mac OS X x64. So, download the dmg file, then, when it completes downloading, double click it, and install like any other software.
What we want to do now is let our operating system know where this installation lives. Once we do this, and other software that you use that would like to use Java, will be able to use this operating system variable to find our Java installation.
These "operating system variables" are known as Environment Variables.
Run these two lines in your terminal:
Now, to check that it has been set correctly, you can run:
and the output should be the location that you just set above.
To see the version of java you have installed, run