cd ~
means change to your home directory (the place designated as yours in UNIX-land, including Mac OSX which is based on UNIX). It's likely to be something along the lines of /home/david or /Users/david if your user name was david.
In UNIX, you have the concept of a working directory, your current location within the filesystem hierarchy, and cd is the command you use to change it. Typically, this is the place programs will look for their files if you use a relative filename, so rm xyzzy will attempt to remove the xyzzy file in your working directory whereas rm /xyzzy will attempt to remove a file of the same name in the top level (root) directory.
And you don't have to compile and run Java that way. It's just one way of doing it. If you have an IDE like Eclipse, you probably never need use the command line at all.
javac is indeed the Java compiler, which will turn your source code into class files, and java is the Java runtime which will actually run those class files.
You can put your Java source code anywhere where you have the privileges to create files, you don't have to put them in your home directory. Of course, if you put them somewhere else, like /home/david/javasrc or /Users/david/javasrc, you'll need to ensure that's the directory you're in when you compile and run them from the command line.
To do that, the cd command would be more like one of:
cd /home/david/javasrc
cd /Users/david/javasrc
cd ~/javasrc