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
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:P)
Re: Curly braces or semicolon?
Ahh I see. Makes sense :P
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
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.
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 :)
Code :
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
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
Re: Curly braces or semicolon?
Yep
Code :
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,
Code :
test.A;
// from test, access A
The same can be used when thinking of package access:
Code :
import java.util.Date;
// from the package java, access the package util, then from the package util, access the class Date and import it
Re: Curly braces or semicolon?
Quote:
Originally Posted by
Json
...
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.
Quote:
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.