Learn Java: Part One

Apr 19, 2012 05:35 PM

So you want to learn the basics of the fantastic language Java, but you don't want to search the Inter-webs looking for a great tutorial. Well you came to the right place, now may I present to you JAVA.

Before We Begin

  • You must have the latest version of Java Development Kit, or JDK. At the time of writing it is JDK 7. Go to Oracle Java SE Download Page and find the version for you.
  • Many Linux distros come with JDK in your package manager so do a simple search for it and install it, for Linux I recommend installing OpenJDK as it is the Open Source version of JDK.
  • Mac OSX typically this is already installed, if not open up your software manager and look for it.

IDE (Integrated Development Environment)

An IDE is near necessary for you to code in any language effectively. I prefer the Eclipse Classic Platform IDE. If you would like to download Eclipse, click here. There are tons of other IDE / SDKs available like Netbeans but for the sake of simplicity I will just use Eclipse.

Okay, now that you have JDK and Eclipse or an IDE of your choosing, lets get started.

Start

Okay, open up Eclipse and if this is your first time you should get something like this, don't worry just hit "OK".

Now we are going to go to File>New>Java Project, then for project name lets call it "HelloWorld".

Then right click HelloWorld in the package explorer like so and then scroll over new then click class then call the class HelloWorld

Then in the middle box we are going to type the following after the first line that is generated ( public class HelloWorld{ )

The yellow underline just means that the object hasn't been defined yet, but we are going to do that now, so don't worry, if you get a red line that means you typed something wrong. If you get a red line and don't know why you might want to look at my code and see if you put in the ';' after each line as that is needed to tell Java that line is done, this is the end to most of all Java code.

Note: public static void main(String args[]){ is needed for every class that is going to be executed. The "//" are comments like how "#" is in other languages it just tells the compiler that that line or set of lines is nothing important and not needed.

Define the objects and output them to the Console

Okay now that is out of the way we are going to do the following,

  • Make the String HelloWorld actually mean something.
  • Make the int number actually define a number.
  • Make the double decimal give us what int number + 5.5 is.
  • Make it give us all this information in the console.

Okay, now if you look at the code that follows what do you notice? You notice that I added some fancy comments and that I don't have the word int, String, or double. And you notice that the console tab has a something in it.

So what this code does it tells Java that HelloWorld means "Hello World, you did it!" in it and the number means '5' and decimal is number+ '5.5'.

The line of code at the bottom that says System.out.println(HelloWorld+" "+number+" "+decimal); tells Java, "Hey guess what we are going to say something to the console. We are going to say what ever this HelloWorld object means a ' ' what number means another ' ' and what decimal means." As it does that it prints it down in the Console so we all can read it.

Basic Math functions

What are we going to do now? Well we are going to:

  • Find the answer of 100*8 and save it as a new int
  • Find 5+6 save it as a new int
  • Find what 9/3 is and save that as a new double
  • Find what 9-12 is and save that as a new int

Whats new in this code? Well here you can see that you can define an object without having to do it on another line. You also see that you can put normal strings of text into a print statement without needed a String.

That's all for now on this, if you liked this and would like more please leave a comment at the bottom and if you are wondering why my Eclipse looks darker it is because Eclipse uses the GTK interface. Well I hope this helped someone learn the basics of Java and some of Eclipse. Play around with this code for a while and see what you can do.

Raw Source Code in-case it is hard to read the code: http://pastebin.com/ZV6Lnafn

Image From: TeechWorld

Comments

No Comments Exist

Be the first, drop a comment!