Wednesday, March 4, 2015

Your First Java Program

Java is used on over 6 billion devices worldwide. It has been in development since the 1990s by Sun Microsystems; and slowly became one of the top languages for software and applet development. Hopefully this post will teach you the basic knowledge necessary to run your first program in Java!

I: Setting Up Your Machine
There are two main types of Java: 'vanilla' java for the everyday user, or the Java Development Kit for users who want to compile their own Java code (that's us!).  If you've had your machine (a fancy word for computer) for more than a few days, chances are you've installed vanilla Java--it's used for just about everything. However, you'll need the JDK, so let's head over to the Java website to download it. Here's the link to the Java 7 SDK downloads, or, alternatively, Java 8 SDK downloads. Simply look in the first chart for your system, and download it. It should install itself.

Tidbit: Wondering which version of Java your machine is running? Open up the console and type "java -version". 

Mini-tidbit: Unsure where your console is? On windows, use Windows Button + R to open the Run prompt, then type "cmd" and hit enter. On OS X, simply open Finder and type in "console". 

Once you've completed the download and installation of the JDK, you're ready to code! There are two routes you can take from here:
  1. Use a simple text editor such as Notepad or Notepad++ to write your Java. (this requires a bit more gritty work to 'compile' your code, more on this later)
  2. Use an 'Integrated Development Environment' or IDE. IDEs provide a streamlined and easy way to edit, compile, and run code with a single click. This is what I recommend you do!
We're going to assume you want to take the easier route, you lazy person, and use an IDE. I highly recommend using IntelliJ's IDEA, which can be found here! Simply download the community version and run the installer. Now we're finally ready to get going!

II: Our First Program
Alright, we're all set up and ready to go! The first thing you should do is open up IDEA and create a new project. Call it "HelloWorld". In Java, it's customary to always use what we call 'camel case', which means that every new word is capitalized. So, "anewprogram" would become "ANewProgram". 

Tidbit: It's worth noting that the first letter is always lowercase, unless we're naming our program. 

At this point, you should have something like this:



Now let's make our first program! Simply right click the blue folder labeled "src", and click New > Java Class. We'll name it HelloWorld as well.

And now we have a beautiful blank file to code!
For your first program, make a new line between the two curly braces and type the following:
Your whole program should now look something like this:

Now the moment of truth! Go to the Run tab and click Run, then hit Enter if prompted! Cross your fingers.
If your box looks like my box, PERFECT! Well done! You've just written your first program! Does it feel amazing? Can you sense the raw power flowing through your fingertips? Good. You should. Tune in next time for an analysis of what in the world we actually typed in that program, and what it means! Until then, try inserting different phrases in those quotation marks, and see what you can get your computer to say!

Until next time,

Nathan

1 comment: