Basically, Java conventions are the proper indenting of your statements, methods and other various things. You can indent things by using the tab however explaining the conventions properly is a tough task. It's easy to grasp however because you just look at an example and you get most of it. You can learn properly about Java conventions by going to the official Java sun page which I'll provide to you here: http://java.sun.com/docs/codeconv/. You'll have to download something but don't even bother to read it basically just look at the examples and you should immeditally know. By the way, you don't HAVE to learn about conventions for your class file to work however it is helpful while rereading the initial file and other's will most likely understand it better. Might as well get into the habit!
Now we'll learn about switch statements, which are useful for long if and else statements. It's proven actually that switch statements have better performance during runtime verses the conventional if statement. So, in this case, there are actually purposes to using it however don't worry, I'll explain it perfectly.
Note, I suggest using switch statements when there is more then around 5 if statements in a row in the same method. Don't over abuse them however don't under use them either. Please also remember that switch statements switch integers only! Alright now that you have successfully distinguished where a switch statement is needed, it's time to begin to actually write your first one!
We always begin to write one like the following:
Of course, you can change the myIntExample to what ever you want, but for now just leave it as it is. This basically says that the switch statement will be added in the body of the switch. You cannot use a switch statement or implement one outside of the switch body, you'd either have to declare a switch or basically not use one. The convenitonal way of if statements would be to write them like this:
However unlike if statements, the switch statement would be something like this:
The API case followed by the value is what happens when the integer, myIntExample is equal to one. So, if that variable is equal to one, it'll do what ever in case value one until the break statement. Basically, the case kind of represents the opening bracket of an if statement and the break statement is the closing bracket. However if you want to remove the break and still leave the break statement on case two, then case one's actions will fall over to case two. It's kind of like a flood; no break then it'd continue on down.
Example:
For a full understanding, please read throughout this code snippet in the quoted out areas. Some of you may learn this way rather then having to read the whole time, which is perfectly fine. Hope you understand switch statements, as they are really useful to have in your knowledge database.
Credits: 100% Mazeingmazerules (Rog3r on other forums)
Now we'll learn about switch statements, which are useful for long if and else statements. It's proven actually that switch statements have better performance during runtime verses the conventional if statement. So, in this case, there are actually purposes to using it however don't worry, I'll explain it perfectly.
Note, I suggest using switch statements when there is more then around 5 if statements in a row in the same method. Don't over abuse them however don't under use them either. Please also remember that switch statements switch integers only! Alright now that you have successfully distinguished where a switch statement is needed, it's time to begin to actually write your first one!
We always begin to write one like the following:
Code:
switch (myIntExample) { }
Code:
if (myIntExample == 1) { // ... } else if (myIntExample == 2) { // ... }
Code:
switch (myIntExample) { case 1: // ... break; case 2: // ... break; }
Example:
Code:
switch (myIntExample) { case 1: // If 'myIntExample' is equal to one.. // No break statement here, continue on down! case 2: // If 'myIntExample' is equal to two... out.println("HELLO!"); break; // Stops the 'flow' case 3: // This does it's own 'actions' since there's a break statement above out.println("WUT"); // It'll print out the line on your run prompt, "WUT" break; // Stops the 'flow' default: // If 'myIntExample' is not equal to 1, 2 or 3, then it'll do this... out.println("I'm lonely!"); break; }
Credits: 100% Mazeingmazerules (Rog3r on other forums)
Last edited by mazeingmazerules (2009-07-08 08:41:26)