The Ultimate Java Tutorial
Mission:To re-engage and teach the users of MPGH the syntax, design, and implementation of the fantastic programming language of Java.
This thread is not the guide. This thread is an intro, and it will have a TOC with links to other threads I will make that will guide you from outputting strings to putting threads to sleep.
Table of Contents
Chapter 1 - Basic Java Syntax
Now, lets begin.
Java takes you to new places. From its humble release to the public as the (wimpy) version 1.02, Java seduced programmers with its friendly syntax, object-oriented features, memory management, and best of all-the promise of portability. The lure of write-once/run-anywhere is just too strong. A devoted following exploded, as programmers fought against bugs, limitations, and, the fact that it was horribly slow. But that was ages ago. If you're just starting in Java, you're lucky. Some of us has to walk five miles in the snow, uphill both ways (barefoot), to get even the most trivial applet to work. But you, why you, get to ride the sleeker, faster, much more powerful Java of today.
The way Java Works...
1. Source - Create a source document. Use an established protocol (in this case, the Java language)
2. Compiler - Run the document through a source code compiler. The compiler checks for errors and won't let you compile until its satisfied that everything will run correctly.
3. Output (code) - The compiler creates a new document, coded into Java bytecode. Any device capable of running Java will be able to interpret/translate this file into something it can run. The compiled bytecode is platform-independant (can run on Mac OS, Linux, Windows, anything that will come out).
4. Virtual Machines - Your friends don't have a physical Java machine, but they all have a virtual Java machine (implemented in software) running inside their electronic gadgets. The virtual machine reads and runs the bytecode.
What you'll do in Java...
1. Source - Type in your source code (.java)
2. Compiler - Compile the source code by running javac (the compiler application). If you don't have errors, you'll get a second document (.class). The compiled .class file is made up of bytecodes
3. Output (code) - Compiled code of .class.
4. Virtual Machines - Run the program by starting the Java Virtual Machine (JVM) with the .class file. The JVM translates the bytecode into something the underlying platform understands, and runs your program.
A very brief history of Java
CSB = Classes in the standard binary
Java 1.02 - CSB = 250. Slow. Cute name and logo. Fun to use. Lots of bugs. Applets are the Big Thing.
Java 1.1 - CSB - 500. A little faster. More capable, friendlier. Becoming very popular. Better GUI code.
Java 2 (versions 1.2 - 1.4) - CSB = 2300. Much faster. Can (sometimes) run at native speeds. Serious, powerful. Comes in three flavors: Micro Edition (J2ME), Standard Edition (J2SE), and Enterprise Edition (J2EE). Becomes the language of choice for new enterprise (especially web-based) and mobile applications.
Java 5.0 (versions 1.5 and up) - CSB = 3500. More powerful, easier to develop with. Besides adding more than a thousand additional classes, Java 5.0 (known as "Tiger") added major changes to the language itself, making it easier (at least in theory) for programmers giving it new features that were popular in other languages.
Code structure in Java
Java consists of objects, classes, source files, and methods.
Source file - A source code file (with the .java extension) holds one class definition. The class represents a piece of your program, although a very tiny application might need just a single class. The class must go within a pair of curly braces - EXAMPLE
Class - A class has one or more methods. In the Dog class, the bark method will hold instructions for how the Dog should bark. Your methods must be declared inside a class (in other words, within the curly braces of your class). EXAMPLECode:public class Dog { // Do stuff here. // By they way, putting "//" before something makes the program ignore that particular line. Its called a "comment" }
Method - Within the curly braces if a method, write your instructions for how that method should be performed. Method code is basically a set of statements, and for now you can think of a method kind of like a function or procedure.Code:public class Dog { voice bark() { } }
Anatomy of a classCode:public class Dog { void bark { statement1; statement2; } }
When the JVM starts running, it looks for the class you give it to command line. Then it starts looking for specifically written method that looks exactly like:
Next, the JVM runs everything in between the curly braces { } of your main method. Every Java application has to have at least one class, and at least one main method (not one main per class, one main per application).Code:public static void main (String[] args) { // Put your code here }
Look at the following code snippet:
Now, lets dissect it.Code:public class MyFirstApp { public static void main (String[] args) { System.out.print("I rule!"); } }
public - Public means that everyone can access it, meaning every developer, and every class.
class - This is a class.. Duh.
MyFirst App - The name of this class
First { - Opening curly brace of the class (This is where JVM begins compiling code)
static - doesn't change. (we will cover this one later, its a lot more than we can fit here)
void - The return type. All methods have to have something they "return". Void simply means it returns nothing.
main - the name of this method
(String[] args) - Arguments to the method. This method must be given an array (covered later) of Strings, and the array will be called "args"
Second { - Opening brace of the method. Everything between this and the next } belongs to the method code.
System.out.print - This says print to standard output. "." seperates classes, packages (collection of classes) and methods. This tells us that the "print" method is in the "out" class of the "System" package.
("I Rule!") - The string (series of words) you want to print.
; - EVERY STATEMENT MUST END IN A SEMICOLON (!
First } - Closing brace of the main method.
Second } - Closing brace of the MyFirstApp class.
This is just to get you started, don't worry about memorizing this yet.
Useful Links
Eclipse - Most Popular Java Compiler/IDE
Netbeans - Personally recommended by Me. My favorite Compiler/IDE
Java Download
Java Development Kit (Required to Compile)
Java SE 6 API References
This thread will be updated with the latest additions to the Tutorial in the "TOC" at the top of the thread.
Last edited by why06; 03-28-2010 at 05:38 PM.
Great Explain of Java
Thx
Fix your conventions:
Code:public class MyFirstApp { public static void main (String[] args) { System.out.print("Bad convention"); } }Code:public class MyFirstApp { public static void main (String[] args) { System.out.print("Good convention"); } }
u should write a full tutorial. is there a good 1 on the net?

382
Woot, someone bumped it for me so I don't have to ,Some of this is word for word straight out of the book "head first into Java"
authors do not like plagiarism, offer credits....Cite the original author maybe?.
Last edited by NextGen1; 04-24-2010 at 04:15 AM.