| We hope you enjoy your visit. You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. Join our community! If you're already a member please log in to your account to access all of our features: |
| The Apex Of Creation; Java™ Programming Guide | |
|---|---|
| Tweet Topic Started: May 29 2006, 12:22 PM (562 Views) | |
| Shadow | May 29 2006, 12:22 PM Post #1 |
|
Programmer
![]() ![]() ![]() ![]()
|
Title
|
![]() |
|
| Shadow | May 29 2006, 12:26 PM Post #2 |
|
Programmer
![]() ![]() ![]() ![]()
|
Prologue Here's my first attempt at a Java™ guide. It's going to start with the basics, OOP and compiling at first, and progressively get more advanced. If there's anything you want included in the guide, let me know and I will see what I can do. |
![]() |
|
| Shadow | May 29 2006, 12:28 PM Post #3 |
|
Programmer
![]() ![]() ![]() ![]()
|
Introduction Legalities before we start: DISCLAIMER: This guide instructs the user on how to create working Java™ applications and applets, as well as executable jars. This guide should be followed meticulously in order to maintain system stability and security. The guide itself is protected and may not be copied, modified, or posted without the author's express permission. Any source shown or attached to this guide is the work of the author, and therefore should not be copied in any way without permission. This guide is to be used as an instructional tool and as such is open for public use. However, solicitation of source is strictly prohibited. This guide also assumes that the user has JDK 1.4 or later installed with the correct bundles. We will be working solely on the Windows© XP platform for now. If there is a request for coding help on other platforms, please contact the moderator. In addition, the user should feel comfortable with running the Windows© command prompt. If there any questions about JDKs or the command prompt, visit the JDK/CMD Help post first, and if you still have questions, contact the forum moderator, Shadow. As with all construction, one should create a solid foundation before reaching for the stars. So, with that spirit in mind, I am going to start very slowly at first, and then pick up the pace. The first item on the agenda: Hello World! This may sound strange at first but once through this first section of the guide, I promise it will make perfect sense. I have attached the source file and compiled class to this post. However, if you truly want to learn Java™, I advise following the guide and using the attached files only if something isn't working correctly. First step, open up your editor. I advise something like NotePad or WordPad if available. Let's jump in and get our feet wet and worry about the details later. In your editor, type the following:
Once completed, save this file as HelloWorld.java and close the editor. You have just created your first Java™ class! Congratualtions! But, what now? You'll notice that if you click the HelloWorld.java icon, all that happens is that it opens the editor. Not very exciting. Well, lets fix that. Open up the command prompt and type the following and replace *your directory* with the filepath of HelloWorld.java:
Inside the folder you saved your Java™ file in, you should see a file called HelloWorld.class. Leave the command prompt open, we aren't quite done yet. "What the heck is a .class file?" Our newly created file is actually a compiled Java™ class. You'll notice you can't open it like the previous file. "But you said the .java file was a Java™ class." That's true, however, the key word in describing our new file is compiled. See, our .java file is a Java™ class... in an uncompiled state. We call .java files source or script files where as the completed .class files are called... get ready for this... classes! "Compiled?" More on that later. Remember the instructions we typed into the editor previously? Well, the problem is that the computer doesn't understand prose. So imagine that our javac command is actually translating the file into something the computer does understand, a Java™ class. "I can't open my .class file. Why?" Much like the computer doesn't understand prose, most humans don't understand the script in a class file. Displaying and/or editing the contents of a compiled class is impractical and in some respects, dangerous. The computer however, can read the file, but only when the correct commands are given, and that's our next step. In the command prompt, type this final command:
Now the words, "Hello World!" should be outputed to the command prompt window. You have just successfully executed your first Java™ program! Upcoming: In the next post, I will revisit our program and explain more in depth what each statement in the editor and command prompt actually means. |
![]() |
|
| Shadow | Jun 21 2006, 01:09 PM Post #4 |
|
Programmer
![]() ![]() ![]() ![]()
|
Introduction (cont.) Our first program is now complete! But, what the heck did we actually do? Let's go back and review our program step by step. Upon opening our editor, the first thing we typed was:
To better explain what is happening, lets break this statement up. The word public is something called a visibility modifier. It effects who can see the code we are typing. There are three such modifiers, public, private, & protected. Basically, by typing one of the following words, we are either allowing or denying access to our code. There will a more in-depth explanation on this a little later. For now though, let's continue on. We then typed class HelloWorld. This is simply declaring the name of the project we are about to build. Typing class says we are getting ready to make a new Java™ class, and HelloWorld is the name the class should have. A class is a program ininofitself. This will explored ad nauseum later on, so for now I will save you the lectures. I realize this is a all a bit confusing at the moment, and that's okay. It's normal to be confused at first. This is a large chunk of information to digest in a short amount of time. If you are still confused after Chapter 5, there may be some problems, but for now, just sit back and do your best. Moving right along, our next section of code was the following:
In short, these type of code segments are known as methods. A method is a block of code created to be called at some time during execution to accomplish a certain task. This is a very broken down explanation of a method, but there is no use in overwhelming oneself with technical information. At least not right away. Again, let's break this statement up. The first part, public is our visibility modifier. Just like before, it is telling who can access our code, and who can't. The next word static is quite a bit more complex. I will go deeper into the complexities of static methods later on, but for now, let's just say the the word static allows a user to use this code without making another class. Confusing I know, but it will get better. The code void is actually a fairly simple concept with amazing possibilities. When a method executes, it gives a certain item or value back to the program that called it. This is called returning and is a large part of Java™ programming. The term void means that this particular method doesn't return anything. The next term main is actually the name of the method we are defining. We are letting the computer know what we call when we want this method to execute. The main method is the most useful of all methods. It allows the program to execute. Every program we create will have a main method. The class containing the main method is therefore known as the main class, and is neccessary for our program to execute. The final words String [] args are actually the parameters. A parameter is something the method needs to execute. It is almost always a form of data or some value the method uses to perform its job. This will be explained further later, but for now, just know that parameters and methods work together. The last segment of code is the body of our method:
This command tells the computer to print the words "Hello World!" to our screen. The exact means of the computer doing this are actully rather complex, so we will save that for later. Now that we have picked the code of our source file apart, we will revist the command prompt commands.
The first command, cd *your directory*, is actually telling the computer to change to the folder where you have your files located at. CD actually stands for "change directory" which means, in short, change to the folder I designate. The next line, javac HelloWorld.java, is actually a compiling statement. We are telling the computer to compile our source file into a class. Compiling is a complex process, and in due time, will be explained, but for now just remember that compiling is the basic translation of prose into code the computer can understand. The rest of the statement after javac is simply telling the computer what file to compile. All that from one small statement! Our final segment is actually the executing statement. We are telling the computer to execute our Java™ class and display the output. Notice we did not include the ".class" file extension on HelloWorld. This is not needed by the compiler and actually causes an error if included. And there you have it! You should now be able to create Java™ applications without thinking and I expect to see some soon... just kidding. Take some time to digest what you have learned and once your brain is back to solid after turning to mush, come back for Chapter 1: Data Types. Upcoming: In the next post, I will review the various data types of the Java™ programming language. |
![]() |
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Java · Next Topic » |





![]](http://z2.ifrm.com/static/1/pip_r.png)




HelloWorld.zip (721 Bytes)
12:23 PM Jul 11