Post by cajones on Feb 13, 2007 3:18:36 GMT -5
So for some reason I decided to do this yesterday... so here goes:
Apparently my board is down... and I don't think it's because of administration, because then it'd redirect me to proboards... so while I wait for server25 to come back (which it had better in the hour), I'm going to try an introductory Java tutorial thingy!
________________
The first thing to know is that java runs using the javaVirtualMachine (jvm), which is designed to allow programs written in java to run on multiple platforms. Naturally microsoft wants to make their own creative modifications, and Sun (java's creator) said no, so now the java runtime environment (jre) is not standard on Windows, so in order to run java programs (not counting applets), one must install the jre.
The java compiler (javac) converts source code into bytecode that the computer can run. The java compiler is found in the java development kit (jdk), which can be downloaded for free at java.sun.com. To avoid crazy path-setting and junk, you can get a version with a program such as netbeans which will make compiling and executing java programs easier (although you'll eventually want to use more native applications such as dos; see the notes for how to use java and javac in dos).
Now, on with the actual programming stuff!
Sourcecode is written in plain text format (notepad, edit, Etc) and saved with a .java extension. (note that some versions of notepad won't save this properly; you can get around this by putting quotes around the file name). A .java file is compiled to a .class file which java can then execute.
And now for a java program...
So what do we get as a result? The output window (dos, or ... whatever you're using to run java) will show the line
Well, you could have guessed that much, so let's explain the weird-looking junk!
public class hello
This is the class definition; without this, java doesn't even recognize the file as a program.
Java programs are classes, and what is inside the class is what happens. Only a few things can be done outside of a class, such as importing other classes for use (which will be pretty useful in the near future).
The {} after the class declaration are the containers for what the class does.
Basically, whenever you have sections of code in java, it'll be contained in braces {}. It is very important to keep track of what braces are open and such, but now I'm just being confusing, so let's move to the next line!
public static void main(String[] args)
This defines the main method of the program; this is the method that is run when the program is executed. There are some confusing things in this declaration, but all you really need to know is what to type there.
But if you want to see what everything in the main declaration means... read this paragraph:
Now, for the actual event stuff!
This has three parts, but it's probably the easiest method to remember, so no panicking!
System - accesses the "System" class. We don't really know what it does, but we don't care so long as it works -- one of the advantages to java's object-oriented design.
out - an object in the System class that deals with output. The dots indicate that out is a child of System.
print() - a method of out, connected by the period. print is a method, and we send what we want to display in the parentheses (). Note that the quotes are important!
Notice that at the end of the print line, there is a semicolon (. All commands in java end in a semicolon. (Notice that the opening and closing of braces aren't semicolon'd, because they aren't commands, but blocks of code).
Confused? I hope not!
Notes...
To use the jdk in dos... you'll want to set the path to include the jdk/bin directory. If that's too confusing... you can memorize the path of the javac program, but that's a lot of trouble... (probably c:\"program files"\java\jdk1.6.0\bin\javac ).
If the path is set properly, you do this to compile a program:
javac filename.java
And to run the program...
java filename
Note that class names must match exactly the name of the file!
And if you're interested in a somewhat simplified version of commandline programming, look for acm.jar, which contains a program package. And ... I can't say much else on that unless you ask, so nya.
Program package simplifies several commands in command line programming and is made for beginners, but it seems to me that it might make it harder to understand more complicated programs that involve things like graphics and sound and ... cool things...
So, yeah. This probably sucks.
Apparently my board is down... and I don't think it's because of administration, because then it'd redirect me to proboards... so while I wait for server25 to come back (which it had better in the hour), I'm going to try an introductory Java tutorial thingy!
________________
Java says go!
The first thing to know is that java runs using the javaVirtualMachine (jvm), which is designed to allow programs written in java to run on multiple platforms. Naturally microsoft wants to make their own creative modifications, and Sun (java's creator) said no, so now the java runtime environment (jre) is not standard on Windows, so in order to run java programs (not counting applets), one must install the jre.
The java compiler (javac) converts source code into bytecode that the computer can run. The java compiler is found in the java development kit (jdk), which can be downloaded for free at java.sun.com. To avoid crazy path-setting and junk, you can get a version with a program such as netbeans which will make compiling and executing java programs easier (although you'll eventually want to use more native applications such as dos; see the notes for how to use java and javac in dos).
Now, on with the actual programming stuff!
Sourcecode is written in plain text format (notepad, edit, Etc) and saved with a .java extension. (note that some versions of notepad won't save this properly; you can get around this by putting quotes around the file name). A .java file is compiled to a .class file which java can then execute.
And now for a java program...
public class hello {
public static void main(String[] args) {
System.out.println("Yo! This program works, as you can plainly see!");
}
}
So what do we get as a result? The output window (dos, or ... whatever you're using to run java) will show the line
Yo! This program works, as you can plainly see!
Well, you could have guessed that much, so let's explain the weird-looking junk!
public class hello
This is the class definition; without this, java doesn't even recognize the file as a program.
Java programs are classes, and what is inside the class is what happens. Only a few things can be done outside of a class, such as importing other classes for use (which will be pretty useful in the near future).
The {} after the class declaration are the containers for what the class does.
Basically, whenever you have sections of code in java, it'll be contained in braces {}. It is very important to keep track of what braces are open and such, but now I'm just being confusing, so let's move to the next line!
public static void main(String[] args)
This defines the main method of the program; this is the method that is run when the program is executed. There are some confusing things in this declaration, but all you really need to know is what to type there.
But if you want to see what everything in the main declaration means... read this paragraph:
- public - Shows that the method can be invoked by running programs. Necessary for the program to run on its own. (Notice something about the class declaration?).
- static - Indicates that the method is static. I don't really know what this means since it's fairly useless in most cases... but it is necessary here.
- void - indicates that the method doesn't return anything. For a main program ihat doesn't do much... basically, if you're writing a method that isn't sending a value to another method, you use void. Our main method uses void because it invokes itself and therefore doesn't return anything to anyone.
- main - just the name of the method, and the one java uses for the running of an application.
- (String[] args) - parameters. Methods come with parentheses () that indicate that they are methods, and apparently main accepts a String[] parameter called args. You'll figure out what String[] is soon enough. And args is just the random name chosen by sun... I'd not risk changing it.
Now, for the actual event stuff!
System.out.print
This has three parts, but it's probably the easiest method to remember, so no panicking!
System - accesses the "System" class. We don't really know what it does, but we don't care so long as it works -- one of the advantages to java's object-oriented design.
out - an object in the System class that deals with output. The dots indicate that out is a child of System.
print() - a method of out, connected by the period. print is a method, and we send what we want to display in the parentheses (). Note that the quotes are important!
Notice that at the end of the print line, there is a semicolon (. All commands in java end in a semicolon. (Notice that the opening and closing of braces aren't semicolon'd, because they aren't commands, but blocks of code).
Confused? I hope not!
Notes...
To use the jdk in dos... you'll want to set the path to include the jdk/bin directory. If that's too confusing... you can memorize the path of the javac program, but that's a lot of trouble... (probably c:\"program files"\java\jdk1.6.0\bin\javac ).
If the path is set properly, you do this to compile a program:
javac filename.java
And to run the program...
java filename
Note that class names must match exactly the name of the file!
And if you're interested in a somewhat simplified version of commandline programming, look for acm.jar, which contains a program package. And ... I can't say much else on that unless you ask, so nya.
Program package simplifies several commands in command line programming and is made for beginners, but it seems to me that it might make it harder to understand more complicated programs that involve things like graphics and sound and ... cool things...
So, yeah. This probably sucks.