Learn Java: Part One

Part One

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.

Step 1 Start

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

Learn Java: Part One

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

Learn Java: Part One

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

Learn Java: Part One

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.

Step 2 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.

Learn Java: Part One

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.

Step 3 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

Learn Java: Part One

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

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

9 Comments

Thanks, I may need to change my GTK theme when I do the next one.

Yeah, that part is a little hard to read. Otherwise solid.

Yay, I hope a lot of people read your tutorial because I write lots of articles using JAVA and I think that might be a barrier to a good portion of people, thanks! As a suggestion, I like it when there is an actual .java file that I can download and import into eclipse for experimentation. When I learned JAVA I found that experimentation was extremely important because it helps you remember it better and be more certain about what was really happening since you can alter it and see for yourself what happens.

No prob, I will prob put them as .java's in the next few, I plan on doing the next one sometime soon been busy finishing my game for comp sci class. Its a rouge like in java kinda like frouge game.

Oh, you did upload it to copy and paste! haha Although, I do prefer importing because sometimes I have several classes, in this case that would be ok though and perhaps slightly simpler for newbies...

http://www.codecademy.com this is an amazing link! its free interactive java courses that you take online. you can sign up for a free account to track progress and earn achievements. Also the webpage give you an interactive command prompt so you dont need to download anything if your just looking around!

Thanks Terminal, that's a really awesome site, I am going to add it to my blog! If I am not mistaken it is actually teaching JavaScript, not JAVA (they are fairly different). Still awesome though!

I remember trying to use this site a few months ago when I was trying to learn JS. As Mr. Newsome pointed out is Javascript and they are both different one of the main differences is that JavaScript is mainly used in websites and all everything that happens is run on your computer when using that script thats why there are addons like noscript so those bits of code can't run if you don't trust the website.

Share Your Thoughts

  • Hot
  • Latest