| Welcome to Chibiwolf. 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: |
| java first application | |
|---|---|
| Topic Started: Apr 15 2008, 05:52 PM (41 Views) | |
| MusicUploader | Apr 15 2008, 05:52 PM Post #1 |
![]()
The Ownager
![]() ![]() ![]() ![]() ![]() ![]()
|
Our first application will be extremely simple - the obligatory "Hello World". The following is the Hello World Application as written in Java. Type it into a text file or copy it out of your web browser, and save it as a file named HelloWorld.java. This program demonstrates the text output function of the Java programming language by displaying the message "Hello world!". Java compilers expect the filename to match the class name. A java program is defined by a public class that takes the form: Source Code In your favorite editor, create a file called HelloWorld.java with the following contents: To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is: javac HelloWorld.java For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code. You're ready to run your first Java application. To run the program, you just run it with the java command: java HelloWorld Sample Run Hello world! The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial. HelloWorld.java To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is: javac HelloWorld.java For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code. You're ready to run your first Java application. To run the program, you just run it with the java command: java HelloWorld Sample Run Hello world! The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial. HelloWorld.java The Java programming language has includes five simple arithmetic operators like are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The following table summarizes the binary arithmetic operators in the Java programming language. The relation operators in Java are: ==, !=, <, >, <=, and >=. The meanings of these operators are: Use Returns true if op1 + op2 op1 added to op2 op1 - op2 op2 subtracted from op1 op1 * op2 op1 multiplied with op2 op1 / op2 op1 divided by op2 op1 % op2 Computes the remainder of dividing op1 by op2 The following java program, ArithmeticProg , defines two integers and two double-precision floating-point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses + to concatenate strings. The arithmetic Java If-Else Statement The if-else class of statements should have the following form: All programming languages have some form of an if statement that allows you to test conditions. All arrays have lengths and we can access that length by referencing the variable arrayname.length. We test the length of the args array as follows: Source Code
Compile and run this program and toss different inputs at it. You should note that there's no longer an ArrayIndexOutOfBoundsException if you don't give it any command line arguments at all. What we did was wrap the System.out.println(args[0]) statement in a conditional test, if (args.length > 0) { }. The code inside the braces, System.out.println(args[0]), now gets executed if and only if the length of the args array is greater than zero. In Java numerical greater than and lesser than tests are done with the > and < characters respectively. We can test for a number being less than or equal to and greater than or equal to with <= and >= respectively. Testing for equality is a little trickier. We would expect to test if two numbers were equal by using the = sign. However we've already used the = sign to set the value of a variable. Therefore we need a new symbol to test for equality. Java borrows C's double equals sign, ==, to test for equality. Lets look at an example when there are more then 1 statement in a branch and how braces are used indefinitely. Source Code All conditional statements in Java require boolean values, and that's what the ==, <, >, <=, and >= operators all return. A boolean is a value that is either true or false. Unlike in C booleans are not the same as ints, and ints and booleans cannot be cast back and forth. If you need to set a boolean variable in a Java program, you have to use the constants true and false. false is not 0 and true is not non-zero as in C. Boolean values are no more integers than are strings. Else Lets look at some examples of if-else: We could add an else statement like so: Source Code // This is the Hello program in Java class Hello {
Source Code Now that Hello at least doesn't crash with an ArrayIndexOutOfBoundsException we're still not done. java Hello works and Java Hello Rusty works, but if we type java Hello Elliotte Rusty Harold, Java still only prints Hello Elliotte. Let's fix that. We're not just limited to two cases though. We can combine an else and an if to make an else if and use this to test a whole range of mutually exclusive possibilities. Lets look at some examples of if-else-if:
|
|
I Loved Her So Much I Saw A Chance And Missed It Now Im Broken ┏┫ | | ┣┓ ┏┓ ┗┫━━ ┃ ━━┣┛ ┣┫ ┃ ━━━━━ ┃ ┏┳┫┣┳┓ ┗━━┳━┳━━┛ ┃ ┃ ━━━━┃ ┃ ┗━┳┳━┛ ━━━━┃ ┗━━━━━━┛┃
| |
![]() |
|
| DarkCannon | Apr 16 2008, 12:44 PM Post #2 |
![]()
Willy Wolf
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
HAHA! I read this in a book somewere. If you copied it tell me what book cause its driving me crazy. |
| [size=14]I AM A BUNNY! FEAR ME! I SHALL NIBBLE ON YOUR CARROTS! RAWR![/size] | |
![]() |
|
| Chibi | Apr 16 2008, 01:12 PM Post #3 |
![]()
Shmell mah finger
![]()
|
I dont care
:Sticky: |
![]() "I'd kill the Jews." -glare- "I'd not kill the Jews, no. I'd toss in a penny and watch them fight to the death. -snickering- I did the same with two Catholic priests but I tossed in a small boy!! And the winner, had to fight Michael Jackson!"%mh%-100%mh% | |
![]() |
|
| MusicUploader | Apr 16 2008, 09:48 PM Post #4 |
![]()
The Ownager
![]() ![]() ![]() ![]() ![]() ![]()
|
lol i dont remember i used to have the book threw it away i just remembered this from it
|
|
I Loved Her So Much I Saw A Chance And Missed It Now Im Broken ┏┫ | | ┣┓ ┏┓ ┗┫━━ ┃ ━━┣┛ ┣┫ ┃ ━━━━━ ┃ ┏┳┫┣┳┓ ┗━━┳━┳━━┛ ┃ ┃ ━━━━┃ ┃ ┗━┳┳━┛ ━━━━┃ ┗━━━━━━┛┃
| |
![]() |
|
| Chibi | Apr 17 2008, 11:13 AM Post #5 |
![]()
Shmell mah finger
![]()
|
Still a good job though. This will help out other members like Sal. He is pathetic at this stuff. |
![]() "I'd kill the Jews." -glare- "I'd not kill the Jews, no. I'd toss in a penny and watch them fight to the death. -snickering- I did the same with two Catholic priests but I tossed in a small boy!! And the winner, had to fight Michael Jackson!"%mh%-100%mh% | |
![]() |
|
| « Previous Topic · Computer Talk · Next Topic » |






![]](http://209.85.48.18/static/1/pip_r.png)







3:59 AM Nov 27