» Site Navigation | | | » Advertisement | | | » Recent Threads | | | | | | | | | | | |  | |  | Basic Java Tutorial |  |
11-14-2007, 09:38 PM
|
#1 (permalink)
| | Banned
FireWrath is offline
Join Date: Oct 2007 Posts: 3,215 Rep Power: 0 | Basic Java Tutorial Well I couldn't find a Java tutorial here, so I thought I'd throw mine at you. Quote:
This is a tutorial on how to program, so I will just assume you already know what to do with the code, and how to compile it. I will first show you some code, and then break it down for easier understanding of it. With that being said: Let's begin. Code: public class Name {
public static void main(String[] ughs) {
System.out.println("Hello World!"):
}
} Ah yes, the ever so famous Hello World program. What this does is it simply shows "Hello World!" without the quotes, of course.
The first line simply gives your class name, and in this case it's Name (duh). The second line is always included in programs. public means that the method could be accessed from outside of the class, but seeing how this is to be a simple tutorial, I won't go into detail about that. Another thing you want to keep in mind for more advanced programming is that word "void." It states that the method isn't supposed to return anything.
The third line is what makes the program print the two words. We have the quotes there to indicate that there are no variable (which will be covered later) used thereThe semi colon tells the program that the statement is finished. The last two letters "ln" before the opening parentheses stands for line. What that does is it makes sure that the next time the program prints on the next line, rather than continuing from where it was ended.
If we had something like Code: System.out.print("Hello");
System.out.print("World"!); We would see HelloWorld! printed, but if stuck the ln to the end of print, we'd get Seeing how the the the first quote indicates the begining of the text, and the second one indecates the end, if we wanted to stick quote(s) in the text, we would how to do it another way, but how? Using a combination of sybols what we called escape characters. The backslash "\" is what allows us to use the double quote. \' would insert a single quote, \" inserts a double quote, \n would end the line, so everything after would start on the next line, much like the "ln" mentioned above. \\ sticks a backslash, and \t is a tab (five spaces). Now let's move to the variables (as promised).
So what is a variable, you ask? It's a memory location that stores a value. So let's look at some code now... Code: public class Name {
public static void main(String[] ughs) {
int number = 0, Number = 1;
double nUmber = 2.01, nuMber = 3;
int numBer = number + Number;
int numbEr = number + nUmber;
double numbeR = number + nuMber;
}
} The reason why I started the counting at 0 is because that's the way computers count. Anyway, Java is case sensitive, so if the variable name is the same, but the capitalization is different, that's ok; however, you cannot use the same variable name more than once. You cannot use certain words for variable names, and public, class, void, main, int, and double are just some of them if you needed an example.
You for variables there, the first two are ints, they are integers (whole numbers), where as double have decimals.
If you print number, you will see 0, whereas if you print out nUmber you will see 2.01. Even though I didn't end nuMber with a number, it will still print out 3.0. Since numbEr is an int, and there's a double in the equation, you can't run the program. NumBer, on the other hand, is an int, and so are the other two variables, so there's no problem. numbeR; however, is a double, so it will have no problem printing any kind of number, so in that case, it would print 3.0. If want to divide, you would use /, to subtract it's -, and to multiply use *. A not as well known character is the modulus operator, which is %. What that does is it finds the remained for an integer, and an integer only. Here's an example Quote:
_2_ R:16
45 |100
-84
----
16
| Note: assignment statements (those that use =) work from left to right, and even though that may not appear very important, it is in the more advanced coding. Typecasting
This is where you change the type your variable is so the program can run without errors. We have agreed that Code: public class Name {
public static void main(String[] ughs) {
int number = 0, Number = 1;
double nUmber = 2.01, nuMber = 3;
int numBer = number + Number;
int numbEr = number + nUmber;
double numbeR = number + nuMber;
}
} Would cause an error if we tried printing numBer, right? If we tweaked numbEr just a bit to Code: int numbEr = number + (int) nUmber; We could print out numbEr without an error and get 3 for our answer.
So now that you know how to print text with variable, and you know how to print text without, you may want to know how to do both, so here's an example for ya: Code: System.out.println("Number = " + Number); The plus there tells the program to combine the text and the variable, otherwise it would expect to be ended, so you would get an error trying to run it. String variables
First off, String veriable hold text values, much like ints and doubles hold number values, and declaring the String variables isn't much harder. Look for yourself Code: String word = "Hello World!"; The major difference is that strings have quotes around them. If you were to print word (the variable), then you would see Hello World! String methods
Strings also have different methods, and to help you understand them, take a look at the word computer. Code: String s = computer; Quote:
C o m p u t e r
0 1 2 3 4 5 6 7
| That's how a computer would count the letters. With that being said, if you wanted to print the letter that is in the fourth slot, you would use: Code: System.out.println(s.charAt(2)); charAt is the method that figures out what letter is in what slot of the string (and keep in mind, since computers start counting from 0, C corresponds with 0, and r corresponds with 7).
Say you wanted to print the length of the string, you'd use Code: System.out.print(s.length()); Since we're not looking at the position of the slots of the characters, that would print the amount of characters, which 8.sub string (should be one word, but the forum doesn't allow it, so I added a space above and below) is the way to go. Code: System.out.println(s.sub string(4)); Once again we're going to character counting, so that would print uter. You can also specify where it end printing by adding a comma and a second number, like System.out.println(0,2); which would only print the first three characters.
Printing out the slot a character is at is not hard, just use the indexOf method. Code: System.outprint(s.indexOf("o")); That would print out 1. Keep in mind that you can look for a phrase, a word, or a full blown sentence in your string; however, if what your looking for is not found, then it will print out -1. If then else
This is simliar to the theory in math class "If p, then q." This has makes a decision based on what you're asking, and if it's true, it does one thing, otherwise it'll do something different. This may be complicated, so here's an example Code: int number = 1;
if (number < 0)
{
System.out.println("Number is less than zero.");
}
else if (number == 0) {
System.out.println("Number is zero.");
}
else {
System.out.println("Number greater than zero.");
} This is pretty obvious, the first line of the if statement is what the program goes for, so if it's true, it'll stop by printing out Number is less than zero. If it's not true, it'll go down to the next step, the else if, and it does the same thing. If that's also false, finally we get to the else, that's saying if everything above is false, this is what must happen. Notice that the else if has two equals signs, that's because a single sign means you're giving the variable a value; therefore, you would use == so it wouldn't think that. When comparing strings; however, you would use the .equals method. Code: String word = "hello", Word = "world";
if (word.equals(world)
System.out.println("word is the same as Word.");
else
System.out.println("Word is no the same as Word.");
if (word.equals("hello")
System.out.println("word is \'hello\'.");
else
System.out.prtinln("word is \'"+word+"\'."); Once again, since it's a string, different rules apply; however, when using that, notice that in the fist case there are no quotes, that's because we're
comparing word to another variable, and in the second one we're not, so therefore we add the quotes. You also may have noticed that there are not braces {} in the if's, that because if you have only one statement after following the if statement, then they are not needed; however, if you were to add another line, then you would need the braces. For loops
Those are control structures which are used when you want to repeat something. Code: int count;
for(count = 1; count <= 5; count = count + 1)
{
System.out.println(count);
} So that first time count appears in the statement above is count given its value. The second determines when the loop stops (as soon as it's false). The third part is what happens to count when everything in the loop has happened, in this case after count has been printed. The output, which is what the user sees when something is printed, will be An easier way of having that third part of the for loop would be to use count+=1; When I told you that assignment statements worked from left to right, I wasn't kidding, and this is a simple example. It says the 1 is being added to count, and that's why when read right to left it makes a bit more sense. This way a lot more efficient because it saves you some time, especially if you're dealing with many for loops. Yet another easier way would be to use count++; (also works with --) but it only increases by one, whereas the += will add any number. While loops
Very similar to for loops, and I perfer them over for loops, but it's your call what you use. Code: int count = 1;
while (count < 10)
{
System.out.print(count);
count++;
} That will keep print out count until it's no longer less than ten, so the output would be 123456789.
You can also have nested loops, meaning a loop inside a loop, and here is an example Code: int count1, count2, sum;
for(count1 = 1; count1 <= 4; count1++)
{
for (count2 = 1; count2 <= 5; count2++)
{
sum = count1 + count2;
System.out.print(sum + " ");
}
System.out.println(); The final output is Quote:
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
| And the same can be done with while loops, and if statements as well. Allowing user input
User input is what the user types in something when asked by the program. This could be a word or number, doesn't matter, just depends on how the program is written. There are several way of doing this, but the method I use is by adding To the very top of the program, that will allow the next command to work, which is added after the equals sign instead of the value like above. Code: int num = input.readInt(); That will wait for a user to type in a number, but if you want the user to be aware of that, you may want to use Code: int num;
System.out.print("Num = ");
num = input.readInt(); Keep in mind that what goes after input.read varies on the type of variable you're looking at, since it's an int, I would add Int() to it, if it was a double, I would add Double(). These have to be capitalize, sine Java is canse sensitive. For strings; however, you would use input.readToken() instead of String.
Well I believe that I have covered enough for now, I may update this, but chances are I won't unless there's a mistake in there somewhere (other than typos, which I told tend to fix).
| | |
| |  |
11-14-2007, 09:58 PM
|
#2 (permalink)
| | Banned
lil is offline
Join Date: Oct 2007 Location: CA Age: 23 Posts: 7,074 Rep Power: 0 | Thanks! Now I know where to begin with java haha. | |
| |
11-15-2007, 12:36 AM
|
#3 (permalink)
| | Banned
Zetin is offline
Join Date: May 2007 Location: South-West Pacific Posts: 478 Rep Power: 5 | Cool +rep, I was looking for one of these | |
| |
11-15-2007, 10:47 AM
|
#4 (permalink)
| | Banned
FireWrath is offline
Join Date: Oct 2007 Posts: 3,215 Rep Power: 0 | Heh, well glad I could help. If you need, feel free to ask. | |
| |
11-15-2007, 11:03 AM
|
#5 (permalink)
| | Underground
Noodle is offline
Join Date: Nov 2006 Location: dorset, UK Age: 15 Posts: 9,142 Rep Power: 18 | This was ripped. But my damn internet won't go the page -_- | |
| |
11-15-2007, 11:21 AM
|
#6 (permalink)
| | Virus lurves Lara
virusdetected is offline
Join Date: Dec 2006 Age: 17 Posts: 6,310 Rep Power: 14 | Lol. The "Hello World!" is the well known beginners tut for Java.
__________________ Quote:
Originally Posted by nomhak No he isn't, most of Viruses girlfriends play in the same elementary school playground. | Current Anime Watchlist:
Elfen Lied - FINISHED T_T <3
D.Gray-Man - Up To Episode 40
Tenchi Muyo - Up To Episode 13
Night Wizard - Up To Episode 5 | |
| |
11-15-2007, 12:46 PM
|
#7 (permalink)
| | Underground
Noodle is offline
Join Date: Nov 2006 Location: dorset, UK Age: 15 Posts: 9,142 Rep Power: 18 | If you google the the first 1/5 it's word to word on another site. | |
| |
11-15-2007, 02:29 PM
|
#8 (permalink)
| | Banned
FireWrath is offline
Join Date: Oct 2007 Posts: 3,215 Rep Power: 0 | That was me, I wrote that tutorial for another forum, and decided to throw it here, which is why I quoted it. If you like, I could edit the post on the other forum to prove that I'm really the one that wrote it. It's insulting that you'd even accuse me of that when it's my own work, and I just wanted to share it to help users. :/
Last edited by FireWrath; 11-15-2007 at 02:33 PM.
| |
| |
11-15-2007, 02:55 PM
|
#9 (permalink)
| | Resident Psychopath.
Fewmitz is online now Join Date: Nov 2006 Location: fewmitz@live.com Posts: 4,744 Rep Power: 15 | Nice man, I was gonna write one of these if I ever got around to learning it, but I guess you beat me to it :o
__________________ | |
| |
11-15-2007, 04:30 PM
|
#10 (permalink)
| | Banned
FireWrath is offline
Join Date: Oct 2007 Posts: 3,215 Rep Power: 0 | I'm currently learning sockets, so when I I'm comfortable with them, I'll update the tutorial as well. | |
| |  | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |