i learned c++ allows legal code like:
12; //just expression -- expression statement exact int = 45; (i > 0) ? i-- : 1; altghough may not best pracitce, can see useful situations in using expression full statement help. so, wondering, why isn't there similar feature in java? reason prohibiting code following:
public static void main (string[] args){ func() + 1; // error: not statement } public static int func(){ return 34; }
altghough may not best pracitce, can see useful situations in using expression full statement help
using expression statement (like system.out.println("hello")) allowed in java. it's made explicit exception expressions side-effect free (like arithmetic expressions). there no situations using side-effect-free expression statement has effect (useful or otherwise).
so, wondering, why isn't there similar feature in java?
you have wrong way around: it's feature in java that's missing in c.
the reason 42; allowed in c , not in java in c standard's grammar definition there's rule <stmnt> ::= <expression> ';'. same rule exists in java, in java added exception makes illegal apply rule expressions side effect free. added rule because code never have effect , since programmer unlikely write pointless code on purpose, it's error , should treated one.
note downside of adding exception makes standard more complicated (by adding exception grammar) , makes implementation harder (because compiler needs detect code , produce error - though should noted most, if not all, c , c++ compilers used today detect code , produce warning this).
Comments
Post a Comment