• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Programming Tutorial Part1 - Ideology, Classes and Objects


New Member
Oct 15, 2006
544 (0.11/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
1. Nothing annoys me more than people who spew out reams and reams of jargon just to make themselves sound clever. Hence, wherever possible in these articles i'll refrain from abusing them - HOWEVER for some of the more advanced concepts it's unavoidable - when these come around i'll be sure to stick in a pretty glossary at the top.
2. I'll try putting some simple questions at the end to test your understanding of what i'm babbling about (if anyone follows these, that is) - just a bit of something different really... Only do 1 per person just incase others come along?

Why Java?
Despite the negative publicity that Java gets (mainly through it's use for writing games for mobile phones and the misconception that it's not powerful enough for anything else) it supports many many many good features that make it very easy for beginners and professionals alike to write amazing code. Oh, and the API's are stellar. Oh, and the best IDE's are free. Oh, and it's fast. In the coming parts i'll write about many of the cool things about Java but for now - on to the basics :)

A little about Java
Java is owned by a company called Sun Microsystems. It was created by a man named James Gosling, who now works for Sun Microsystems. He's a dude.

Java is an interpreted language, so when you've written you code and you compile it, you get what's called byte code. This byte code is then run inside the Java Virtual Machine which is basically a glorified emulator XD. It reads the byte code and acts upon it to effectively run your program. The advantage to this is that Sun Microsystems slave away to create Java Virtual Machines that run on pretty much every piece of hardware/operating system going - so we don't have to :) Once you've compiled your program it'll run on any Java Virtual Machine (ok, i'm calling it JVM from now on!) on any hardware on any operating system. Big bonus imho. In most other languages you have to spend hours getting code to run on Linux if it was originally written for Windows.

Java is an Object Oriented programming language (aka OO programming language). There are two main families of programming language out there - Functional programming languages and Object Oriented programming languages. Examples of functional languages are Pascal, Haskell, the forumla bar in MS Excell is a form of functional programming aswell. I won't go into functional programming languages because they're pretty nasty - a big plus for them is that you only need 2-3 lines of code to do something that might take 20-30 lines of OO code. Examples of OO languages are C++, C#, Java. Check wikipedia for comprehensive lists ;)

Ideology of Object Oriented Programming - OOP
The object oriented programming style dictates that you split up the problem you are working on into several smaller chunks. You then break up those chunks into even smaller chunks. And again, and again, until each chunk is so simple that programming it is dead easy. You start by splitting up your problem into distinct groups, then decide what each group should be responsible for:

You want to model a library inside a computer system. I would begin by thinking "At the core of a library system are the Books. So i'll start by modeling a Book. Then i'll model a shelf of books. Then an isle. Then a library wing." etc. In this fashion, modeling a book is easy - a book might have a title, author, content. Similarly, a shelf is just a collection of several Books. An isle is a collection of shelves, etc. The problem has been dissected into several smaller problems - each on it's own is a trivial thing. If you try and model a library straight up without this level of abstraction it would be pretty darn complex.

What is a Class?
When you create a Java program you concentrate on creating Classes. A Class is responsible for delivering a specific piece of functionality. In the example above, the Classes would be "Book", "Shelf", "Isle", "Wing" etc. So each Class contains some code that helps abstract on the overall problem that you are trying to solve. From a Class definition, you can create Objects at runtime(when your code is actually running).

What is an Object?
An Object is a "real" form of a Class. In the above example, if you have a Book class then an Object of class Book might be "Shakespear", or "The Oxford English Dictionary". Objects only exist at runtime - they follow the code you have written into their corresponding Class file.

Comparison between Classes and Objects
A Class is a definition of some functionality. An Object is an instance of a Class. You write a Class definition once, and then you can create as many instances of that Class as your system requires.

Class           | Example of Object
Book              Shakespear
Shelf             2-B
Isle              7
Wing              West wing
VideoGame         Assassins Creed
Drink             Diet Coke
Pizza             Margheritta
GraphicsCard      HD 3870XT
MobilePhone       Nokia 3220
Person            Fred Bloggs
Address           19 Fake Street
So in the library example, you would write Classes for Book, Shelf, Isle, Wing. Then at runtime you create many Objects of all 4 Classes to populate your system.

Out of each of the three things, decide which is the Class and which 2 are Objects of that Class.
1. Gillette Deoderant Lynx
2. Pen Paper Stationary
3. Flower Rose Poppie
4. Weapon Sword Dagger
5. Gun FAMAS P90
6. Ferrari Lambo SportsCar

Think up some of your own!

Next Time...
Structure of a Class - Constructor, Fields, Methods.
Setting up Netbeans
Using Netbeans