First, go here:
Click on JDK download, and download the correct installtion for your machine. For this tutorial, we will use the tar.gz version.
So if you are using a 64bit linux distribution, the file will look something like: jdk-7u40-linux-x64.tar.gz (version permitting)
What we want to do is extract the contents, then 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 installtion.
These "operating system variables" are known as Environment Variables.
Once you have downloaded the file, open terminal, cd to where the file is located. Good practice is to first move the file to a better location than Downloads. So:
sudo mv jdk-7u40-linux-x64.tar.gz /usr/local
now you are in the correct directory, and so is your java download. To extract the contents, issue the command:
tar xzf jdk-7u40-linux-x64.tar.gz
Now, add or change these two lines in your ~/.profile to point to the installation:
export JAVA_HOME=/usr/local/ jdk-7u40