Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By dlorde

Thread: new to Java

  1. #1
    holtaf is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Question new to Java

    Hey guys! I'm just yesterday turned to Java and I have some "problems".
    I am C++ programmer, and I know that in C++ you can do such things
    Java Code:
    if(some_condition)
     statement1, statement2 , ... ;
    But how I discovered , you can't do that that in Java( If I'm not mistaken ). Is there any alternative ways to do that ?
    In example I can to do such thing in C++.

    Java Code:
    ( n > 10 ) ? doit1(), doit2(), doit3() : doNothing();
    But I wonder how can this written in Java ??
    I know that I can do that in this way :
    Java Code:
    if( n > 10 ) {
     doit1();
     doit2();
     doit3();
    }
    else doNothing();
    but I prefer the previous one.

    Whats the reason that Java developers weren't included the comma symbol ?

    Thanks in advice

    -holtaf

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java has a similar ternary operator, but it doesn't have exactly what you want. I guess that the creators of Java didn't feel that it needed that particular syntactic sugar.
    Last edited by Fubarable; 07-01-2011 at 06:52 PM.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You'll find Java generally has a philosophy of KISS (Keep It Simple...), emphasizing readability and maintainability over economy of expression, and preferring single-task/role-per-element (line, method, class) over multiple-task/role-per-element. This seems to be widely becoming accepted as good or best practice in programming generally. In Java it's a reaction against some of the abuses and poor practice that the extreme flexibility of C++ has led some programmers to (e.g. counter-intuitive overloaded operators, over-complex expressions, unmaintainable templates, mind-boggling pointer manipulations, etc). It's easy to write poor code in any language, but Java was intended to minimize the opportunities for shooting yourself in the foot, and this extends to its coding philosophy. In this light, the 'if..else' construct is typically simpler and clearer than a heavily loaded ternary expression. Java coding guidelines and conventions often recommend that the ternary operator is reserved for simple and obvious uses such as conditional assignment or initialization using simple expressions (rather than simply a compressed if..else) e.g.
    Java Code:
    int x = (condition) ? expr1 : expr2;
    
    // which is clearer and less distracting than
    
    int x = default;
    if (condition) {
       x = expr1;
    }
    else {
       x = expr2;
    }
    I've seen some guidelines that ban it altogether, but IMO that's unfounded - it's in the language and can be useful (as above).

    I also found this change of emphasis a little awkward when I moved from C++, but if you can appreciate the pragmatic philosophy behind it, it makes a lot of sense. YMMV ;)

    Some general Java coding conventions and guidelines from the language developers can be found here, and a discussion of the differences between Java and C/C++ (for C/C++ programmers) here.
    Last edited by dlorde; 07-02-2011 at 12:04 AM.
    Fubarable likes this.

  4. #4
    holtaf is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for reply!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    I'm hopping in a bit late, but nevertheless: if Java would allow the comma operator for expressions (just as C and C++ do), it would complicate the grammar, especially for overloaded methods; e.g. see the following method definitions:

    Java Code:
    public void foo(int a, int b) { }
    public void foo(int a) { }
    now the method calls foo(42) and foo(41, 1) are easily recognized, but what if the comma operator were allowed in expressions? both method calls could resolve to the second (one parameter) method but also the first method applies. Parenthesizing the parameter is a kludge: f((41, 1)) and disallowing the comma oiperator in parameter lists isn't the most beautiful solution either (as C++ had to do).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •