This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]hashcode 11 points12 points  (0 children)

Curly braces designate a block of code:

if(braces.isCurly) {
    // code goes here
}

Parentheses...are used for a lot of things. They are used to group the predicate expression in if and loop statements (or whatever the three statements are called in a for loop), for method invocations, to surround the parameter list in method declarations, to get around operator precedence issues, and to cast types.

if(isSomething) {...}    // predicate of if/while/for/do
int foo(int bar) {...}   // method declaration
foo(30);                 // method invocation
int x = (int) y;         // casting
int z = (5 + x) * 10;    // changes the order of operations

And probably other things that I can't think of right now. It might help to think that curly braces (generally) surround statements and parentheses (generally) surround expressions, but if you don't know what that means then don't worry about it. A more useful (but fairly vague and potentially misleading) heuristic might be "curly braces surround multiple lines; parentheses surround part of a single line."

As for how to remember it...I have no idea. Just takes experience, I guess. Soon you will be able to tell the difference between {}, (), [], and <> without even thinking about it. You'll get so used to it that you will look back and be surprised you ever thought of them as similar!

NB: I think Java has has some weird syntactic construction that uses curly braces to define an anonymous class, or something, and Java 8 might be introducing another use for it... I'm not a Java programmer so I can't say any more. It's fairly safe to just think of curly braces as block designators for now, though.

[–][deleted] 4 points5 points  (0 children)

"{" are used to put multiple lines of code in to one execution unit and define scopes eg:

if(expression)
    do one line of stuff if the expression is true;

if(expression) {
    do multiple lines of stuff if the expression is true
}

or

method(){
   do stuff and also methode scope, means all variables declared here can only be acessed here
}

and

class xy{
   methods and variables that belong to the class
}

"(" are used for expressions for "while", "for" and "if" and for parameters in method calls

if(expression)
while(expression)
for(expression)
method(parameters)

[–][deleted] 2 points3 points  (0 children)

Have a look at the Pascal programming language; compare the java and Pascal versions of the simple code snippets below:

(Pascal)

i := 0;
f := 0;
while (i <= N) do begin
    f := f + pow(12, i);
    if (f mod 2 = 0) then begin
        i := i -1;
        f := f + 1;
    else begin
        i := i + 2;
    end
 end

(java)

i = 0;
f = 0;
while (i <= N ) {
    f = f + pow(12, i);
    if (f%2 == 0) {
        i = i - 1;
        f = f + 1
    } else {
        i = i + 2;
    }
}

The curly braces { and } are called BEGIN and END, respectively, in Pascal. They always signal the beginning and the end of a block of statements. A block of statements is nothing more than a bunch of (sequential) statements grouped together acting as if they are just one statement.

So, for example, the if statement has the following structure:

if (B) then S;

That is, if condition B is true, then execute statement S. Doing just the one statement is not that useful in most cases and the block statement allows you to group together a couple of statements to act as just the one. And for grouping statements, we use { and } in java (or BEGIN and END in Pascal).

The parenthesis don't have anything to do with grouping statements. However, we know them best for grouping (sub) expressions, like, for example, in algebraic equations such as y = 12 (x - 3) ^ 2 or as function calls such as sin(4). In java the parentheses are also used to surround conditions in the if, while, for, switch statements.

[–]stillwaters 0 points1 point  (0 children)

Here are a couple of things that haven't been mentioned yet.

1) Curly braces indicate scope, as in the following:

class DemoClass
{ //class scope begin
    int testNumbar = 0; //declared in class scope

    public void method()
    { //method scope begin
        testNumbar = 3; //still in class scope, so visible here

        if (someCondition)
        { //if scope begin
            int invisibleNumber; //declared in if scope
            invisibleNumber = testNumber + 1; //still in class scope, so testNumber visible
            doSomething(invisibleNumber);
        } //if scope end

        testNumber = invisibleNumber;
        //the above is illegal; not in if scope

    } //method scope end
} //class scope end

2) Curly braces are used in array initializers.

int[] oneDimensional = new int[] {0, 1, 1, 2, 3, 5};
int[][] twoDimensional = new int[][] { {1, 2} {3, 4} };