Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Curly braces or semicolon?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Curly braces or semicolon?

    Was just a thought, When do you use curly braces and when do you use a semicolon?

    From just looking at various examples i'm guessing a semicolon is used for a method that executes or something that has to be executed and a brace is used for class declarations...is that right?


    Stacey


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Curly braces or semicolon?

    No stacey,

    Semicolon is used to set the end of the line, like you use a point (.) at the end of a sentence. The semicolon is our way of telling the system here is the end of the line. And if you don't use the semicolon, the system will give you a clear error message.

    The curly braces are to indicate a block of code. And if you use loops and those things: if(), while(), for(), switch() etc that the code in the next block belongs to that loop for example.

    You can use the curly braces also separated without those loops etc and indicate a "stand-alone" block of code (recently discovered this one actually)

  3. The Following User Says Thank You to Bryan For This Useful Post:

    JavaPF (April 28th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Curly braces or semicolon?

    Ahh I see. Makes sense

    While you reminded me...what's the whole story for the (.) operator? My course book is talking about it but i'm not quite getting what it's on about.


    Stacey

  5. #4
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Curly braces or semicolon?

    To be honest, I know when to use it but its hard for me to explain. But i'll give it a shot.

    With the point operator you can access packages, classes and members.

    Skylab.units.users.User; //Skylab, units and users are packages and User is a class.
    User.getId(); //After the object you can use the point operator to access its members.

    Anyone can jump in and correct me if I'm wrong.

  6. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Curly braces or semicolon?

    I'm not sure what you mean "whole story" about the dot operator, its just used as a separation.

    Object/Classes
    They use this to access members and methods.

    Packages
    They use dots to represent folder structure, each dot is effectively a slash.

    I'm not sure why Java uses the dot but the dot itself can not be apart of a package name, its merely a separator and a dot can not be used as a class name, method name or as a member name.

    I guess the dot just makes a lot of sense that way


    When it comes to curly brackets and semi colons its true what Bryan says, the semi colon basically ends a line of code, while the brackets control a block of code.

    Other places I use brackets is when I do OpenGL programming. I won't go into what the block actually does but you can clearly see that I call glBegin and then start a block just because I know that the code in the block is dependent on the glBegin begin called and I like to have the code indented. My IDE then takes care of that for me when I use the curly wurlies

               gl.glBegin(GL.GL_TRIANGLE_STRIP);
                {
                    gl.glTexCoord2d(1, 1);
                    gl.glVertex3f(this.position.getX() + (size / 2), this.position.getY() + (size / 2), this.position.getZ());
                    gl.glTexCoord2d(0, 1);
                    gl.glVertex3f(this.position.getX() - (size / 2), this.position.getY() + (size / 2), this.position.getZ());
                    gl.glTexCoord2d(1, 0);
                    gl.glVertex3f(this.position.getX() + (size / 2), this.position.getY() - (size / 2), this.position.getZ());
                    gl.glTexCoord2d(0, 0);
                    gl.glVertex3f(this.position.getX() - (size / 2), this.position.getY() - (size / 2), this.position.getZ());
                }
                gl.glEnd();

    // Json

  7. #6
    Junior Member
    Join Date
    Apr 2010
    Location
    Inverness - Scotland
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Curly braces or semicolon?

    Sorry I was unclear when I asked about that dot operator..I actually meant how does it work when it's used to access methods or variables of another class. I'm not quite sure how to use it. Do you just put the name of the method(dot)then the name of the method you're trying to invoke/gain access to?


    Stacey

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Curly braces or semicolon?

    Yep

    public class Test
    {
         public int A;
     
         public Test()
         {
              A = 5;
         }
     
         public static void main(String[] args)
         {
              Test myTest = new Test();
              // access the field A and print it out
              System.out.println(myTest.A);
         }

    a simple mental replacement for the dot operator is "from <var>, access <field/method>". So,

    test.A;
    // from test, access A

    The same can be used when thinking of package access:

    import java.util.Date;
    // from the package java, access the package util, then from the package util, access the class Date and import it

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Curly braces or semicolon?

    Quote Originally Posted by Json View Post
    ...
    I'm not sure why Java uses the dot but the dot itself can not be apart of a package name, its merely a separator and a dot can not be used as a class name, method name or as a member name.
    The dot operator is not merely a separator, it is an access operator that signifies that you are accessing the item that appears on the right within the item that appears on the left.

    When it comes to curly brackets and semi colons its true what Bryan says, the semi colon basically ends a line of code ...
    No, the end of line indicator is the carriage return and/or line feed characters (depending on the OS). The semicolon is a separator, typically used to separate statements. It's important to distinguish between lines and statements. Java basically ignores whitespace such as line terminators.