Let’s start by saying that I believe Java is a great language to start learning how to program.
The reasons being:
- Java inherently forces you to learn OOP concepts
- Java applications run basically on any device
- Java is old, and as such it is a well documented language with plenty of resources to learn from
However, having dabbled in logical and functional programming I can say that languages like Prolog and Lisp might be better starting languages to start with as they will help you develop logical reasoning rather than force you to think like a machine.
Beside the premises, let’s begin working with Java.
The first thing you’re going to need to do is install the Java SDK for you to be able to compile and then run your Java Classes (we’ll talk about those in a few minutes)
Just Google Java SDK Download and you’ll easily find the downloadables for your machine.
From my experience, beside downloading and installing, there is nothing else that you need to do if you are using a Mac.
However if you are running Windows you’ll have to add Java and Javac to your environment variables to have them being accessible from any folder while using the Command Line.
Yes, we are going to be using the Command Line.
Worry not, you’ll have to learn at most 2 commands to move around directories and 2 other commands to “compile” and run your code.
To add Java and Javac to your environment variables do the following:
- Find the Java installation folder (most like in C://Programs/Java/jdkNUMBEROFJDK/bin/)
- Copy that folder address (you need to copy the address of the folder that contains Java.exe and Javac.exe, that’s why you’ll have to look for the bin subfolder)
- Go to your Control Panel -> System -> Change Settings -> Advanced -> Environment Variables
and add that folder to your PATH environment variable.
If you don’t have a PATH environment variable, go ahead and create it
Now you are ready to start dabbling in the world of Java programming.
Your first Java Class
Let’s be fancy and go ahead and write an HelloWorld application.
If you don’t have a Programming Text Editor (no IDE’s for rookies!), go ahead and pick the one you prefer between:
Sublime isn’t technically free, however the evaluation period lasts forever.
I suggest you try all of them out and decide for yourself.
Personally I’d go with Notepad++ to start easy and simple and then use Atom if you have to work with big projects, however I’m sure someone that uses Sublime daily will be able to counterargue and tell you that you can achieve the same results just with Sublime.
Also with Emacs you can basically do anything with a single type of a command provided you have the patience to learn how to use it.
Now that you got an IDE, let’s build your first Class.
For right now just follow what I tell you, I’ll explain the stuff later.
- Create a new file, call it Hello.java (if you can’t see extensions click this)
- Open said file and write:
- Save the file
- Now open your Command Line, move toward the folder in which you saved the file (to navigate both on a Mac and Windows you can use cd)
- Type in the Command Line:
- Now you’re going to see a new file appear, called Hello.class
- Type in the Command Line:
Automagically the code is going to be executed and you should see “Hello World” appear on your Command Line.
Conglaturation! You have produced your first Class!
I’m now going to resist the urge of telling all about what you just did and instead I’m going to explain the barebones of what you need to know and only after I’ll write an optional section detailing things you don’t really need to care right now.
The Barebones stuff
- You write your programs inside of files that you call NameOfTheFile.java
- You “compile” those files into “programs” by using javac NameOfTheFile.java on the Command Line
- You run the “compiled” “programs” by using java NameOfTheFile
In order for any single file to work you have to write inside of the file:
To facilitate learning, I’d like you to deal with the fact that you have to write those 2 lines of code each time you want to create a new Java “file” and leave it at that.
Feel free to learn more by reading at the end of the article though!
The command you wrote:
Basically makes Java print the “String” contained within the parentheses to the Command Line.
A “String” can be logically though of as a sequence of characters. However, it will get a little more complex in the future so let me spare you the whole deal for now.
Supplement: The whole deal.
As you might have noticed I placed a lot of terminology between quotation marks, that’s because most of the terminology was used to make it simpler for you to understand the meaning while not being to caught up in the words.
Beside the whole “does Java compile or intepret it’s code” debate, when you wrote the Hello.java file you didn’t just write a program, you wrote a Java Class, which is a beast deserving at least one lesson of it’s own to discuss.
Also note that the command
Doesn’t translate the code to machine-language, rather it turns it into “Java Bytecode” a type of file that is used by the Java Virtual Machine to run your Class.