• Index » 
  • Community » 
  • Tech » 
  • [Java] Conventions, if Statements & Switch Statements Explained
mazeingmazerules
Member
+8|6740|Canada
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:

Code:

switch (myIntExample) {

}
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:

Code:

if (myIntExample == 1) {
        // ...
} else if (myIntExample == 2) {
       // ...
}
However unlike if statements, the switch statement would be something like this:

Code:

switch (myIntExample) {
        case 1:
                 // ...
                 break;
        case 2:
                 // ...
                 break;
}
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:

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;
}
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)

Last edited by mazeingmazerules (2009-07-08 08:41:26)

Gooners
Wiki Contributor
+2,700|6894

Give this guy a Sticky and let him update with all these guides.
.Sup
be nice
+2,646|6715|The Twilight Zone

Gooners wrote:

Give this guy a Sticky and let him update with all these guides.
+tech badge
+2 cookies
https://www.shrani.si/f/3H/7h/45GTw71U/untitled-1.png
GC_PaNzerFIN
Work and study @ Technical Uni
+528|6676|Finland

If I wanted to to learn JAVA, I would read few nice uni books about it.

Do you really want ppl to post 5000 pages of code pieces here about all the huge number of programming languages?

Last edited by GC_PaNzerFIN (2009-07-08 08:42:51)

3930K | H100i | RIVF | 16GB DDR3 | GTX 480 | AX750 | 800D | 512GB SSD | 3TB HDD | Xonar DX | W8
mazeingmazerules
Member
+8|6740|Canada

GC_PaNzerFIN wrote:

If I wanted to to learn JAVA, I would read few nice uni books about it.

Do you really want ppl to post 5000 pages of code pieces here about all the huge number of programming languages?
Myself learn't from reading from average Java programmers, not those complicated books that you find in stores.  Here, if you have any questions you can just post and actually get advice from the person who created the tutorial and chances are, you'd understand it better.  If you'd like to learn with eBooks, then that's your thing although your opinion is personal.  Other's may find this beneficial.

P.S - I'll create one massive tutorial with all of my tutorials inside if your so concerned.  Just notify me via PM and I'll do it.

Last edited by mazeingmazerules (2009-07-08 08:52:03)

Freezer7Pro
I don't come here a lot anymore.
+1,447|6459|Winland

mazeingmazerules wrote:

GC_PaNzerFIN wrote:

If I wanted to to learn JAVA, I would read few nice uni books about it.

Do you really want ppl to post 5000 pages of code pieces here about all the huge number of programming languages?
Myself learn't from reading from average Java programmers, not those complicated books that you find in stores.  Here, if you have any questions you can just post and actually get advice from the person who created the tutorial and chances are, you'd understand it better.  If you'd like to learn with eBooks, then that's your thing although your opinion is personal.  Other's may find this beneficial.

P.S - I'll create one massive tutorial with all of my tutorials inside if your so concerned.  Just notify me via PM and I'll do it.
Just make one thread and post everything there. There's an "edit" button there for a reason.
The idea of any hi-fi system is to reproduce the source material as faithfully as possible, and to deliberately add distortion to everything you hear (due to amplifier deficiencies) because it sounds 'nice' is simply not high fidelity. If that is what you want to hear then there is no problem with that, but by adding so much additional material (by way of harmonics and intermodulation) you have a tailored sound system, not a hi-fi. - Rod Elliot, ESP
mazeingmazerules
Member
+8|6740|Canada
I'll rewrite a bunch of shit anyways.
Freezer7Pro
I don't come here a lot anymore.
+1,447|6459|Winland

mazeingmazerules wrote:

I'll rewrite a bunch of shit anyways.
That still doesn't justify three different threads for what is essentially the same thing, and in my opinion would actually work better when condensed into one.

Last edited by Freezer7Pro (2009-07-08 09:14:58)

The idea of any hi-fi system is to reproduce the source material as faithfully as possible, and to deliberately add distortion to everything you hear (due to amplifier deficiencies) because it sounds 'nice' is simply not high fidelity. If that is what you want to hear then there is no problem with that, but by adding so much additional material (by way of harmonics and intermodulation) you have a tailored sound system, not a hi-fi. - Rod Elliot, ESP
  • Index » 
  • Community » 
  • Tech » 
  • [Java] Conventions, if Statements & Switch Statements Explained

Board footer

Privacy Policy - © 2025 Jeff Minard