Hello there!
Alright so now we get to some of the basic code structure of Java!
First we have the source file.
- Inside your source file you put your classes.
-Inside your classes you put your methods.
-Inside your classes you put your statements.
A source file is a file with a .java extension.
Then inside your source file you have (this would be in your text editor):
(P.S. these statements should be tabbed but blogger takes them out. I'll get on it)
public class Heater {
void heat() {
1stStatement;
2ndStatement;
}
}
So let's make the traditional initiation program "Hello World".
We know it must have a source file, class(es) , method(s) and statement(s).
file HelloWorld.java (in text editor save it as a .java)
public class HelloWorld {
public static void main (String[] args) {
System.out.print("Hello World");
}
}
Alright let's break that down...
public class HelloWorld {
-the public means it can be accessed by all
-class means it is a class...
-HelloWorld is the name of the class
-you must open the class with a brace {
public static void main (String[] args) {
- once again public means it can be accessed by all (as opposed to private which we'll get to)
- static we'll do later
- void means it is a method with no return type
- main() is the name of the method, the main() method always runs first!
- (String[] args), this is a line that creates an "array" of strings and were calling that array args and is a parameter of main(). This allows for information to be entered into the program from the command line as far as I know.
- you must open the method with a brace {
System.out.print("Hello World");
-System.out.print puts the string(characters, letters, numbers strung together) that is in the parenthesis () and quotes "" into the command line.
- you must end a statement with a semicolon ;
Then you finish up by closing the method then the class.
Now we want to run this.
First we must compile this into a .class file.
Navigate to the folder your file is in in command prompt (unsure for mac)
Type dir to check that the .java file is in the directory.
Then now that we have the Javac command because of the last post type:
javac HelloWorld.java
If there are no errors in your program this will compile. Otherwise your going to get some odd messages. I'll put some common errors and errors I made when I first did this.
- Java is case sensitive, capitols matter!
- All statements must end with a semicolon!
- Your string must be within quotes!
- You must open classes and methods with opening braces!
- You must close classes and methods with closing braces!
Alright so if you have your .class file now you can type (still in the same directory):
java HelloWorld
and voila! You get the uber-exiting message Hello World.
Now that you have that you can edit your original code to make that say whatever you want. If you want the message to appear on new lines you just have to put in another identical statement with a different string. Have fun!
Showing posts with label Command Line. Show all posts
Showing posts with label Command Line. Show all posts
Dec 17, 2007
Dec 16, 2007
The Basic Tools
Okay so lets get the stuff you'll need to run some Java programs!
Alright so first if your on Windows you'll need Java 2 Standard Edition SDK which can be downloaded here, just click on the one for JDK 6 Update 3. If your on a Mac you have it already.
When you are writing Java programs write them in a text editor that you can make the file a .java, the simplest ones like notepad are good.
You now need to set the PATH variable of your computer, follow this guide (windows) for that because I found it rather annoying. This is so you can use the Javac command in command prompt to run your program.
I have no clue how to change the PATH variable for Mac or if there even is a PATH variable but this may help because I believe that Mac runs on UNIX. Scroll down to Common Error Messages on UNIX Systems it has an explanation.
Now I find most of that annoying to the extreme but the books and tutorials I have read all say that you should start off running programs in the command line. Personally I think that you should download Eclipse. It is far easier to tell where you made errors and far easier to correct them resulting in faster learning I believe (if you can't find your mistakes and don't know what they are you can't correct them right?).
Here is a link to pretty much the ultimate reference guide for the Java API(explained later, bookmark this you'll need it eventually).
So now you have all that you will need.
Comment if you need help.
Bye
Alright so first if your on Windows you'll need Java 2 Standard Edition SDK which can be downloaded here, just click on the one for JDK 6 Update 3. If your on a Mac you have it already.
When you are writing Java programs write them in a text editor that you can make the file a .java, the simplest ones like notepad are good.
You now need to set the PATH variable of your computer, follow this guide (windows) for that because I found it rather annoying. This is so you can use the Javac command in command prompt to run your program.
I have no clue how to change the PATH variable for Mac or if there even is a PATH variable but this may help because I believe that Mac runs on UNIX. Scroll down to Common Error Messages on UNIX Systems it has an explanation.
Now I find most of that annoying to the extreme but the books and tutorials I have read all say that you should start off running programs in the command line. Personally I think that you should download Eclipse. It is far easier to tell where you made errors and far easier to correct them resulting in faster learning I believe (if you can't find your mistakes and don't know what they are you can't correct them right?).
Here is a link to pretty much the ultimate reference guide for the Java API(explained later, bookmark this you'll need it eventually).
So now you have all that you will need.
Comment if you need help.
Bye
Labels:
API,
Command Line,
Eclipse,
GreyM,
J2SE,
JAVA,
Javac,
PATH variable,
Starting Off
Subscribe to:
Posts (Atom)