Results 1 to 5 of 5
Thread: new to Java
- 07-01-2011, 05:33 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
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
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 ?Java Code:if(some_condition) statement1, statement2 , ... ;
In example I can to do such thing in C++.
But I wonder how can this written in Java ??Java Code:( n > 10 ) ? doit1(), doit2(), doit3() : doNothing();
I know that I can do that in this way :
but I prefer the previous one.Java Code:if( n > 10 ) { doit1(); doit2(); doit3(); } else doNothing();
Whats the reason that Java developers weren't included the comma symbol ?
Thanks in advice
-holtaf
-
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 05:52 PM.
- 07-01-2011, 10:59 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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.
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).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 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-01-2011 at 11:04 PM.
- 07-02-2011, 07:44 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Thanks for reply!
- 07-02-2011, 08:56 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
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).Java Code:public void foo(int a, int b) { } public void foo(int a) { }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks