The Java Statements
Home ] Up ] [ The Java Statements ] Differences from C/C++ ] The "foreach" Loop ]

 

 

Here is a list of Java statements, with some details on how they differ in some cases from C/C++:

Statement In C? In C++? Comments
The empty (null) statement Yes Yes  
Labeled statements Yes Yes May be referred to from break or continue
Assignment expression Yes Yes  
Increment and decrement expressions Yes Yes  
Method invocation No Yes Java does not support global functions

C does not support objects/classes

Java only allows certain forms of expressions to be used as expression statements

Class instance creation expression No Yes Java is richer in this area than C++

C does not support objects/classes

if, while, and do/while statements Yes Yes C and C++ are very cavalier about what is allowed in the conditional expression.

Java requires the condition to be of type boolean; for example, the following is invalid in Java:

int i = 10;
while (i--) 
{ /*...*/ }
switch statement Yes Yes Java has eliminated some very dubious behavior in switch statements that is present in C and C++
for statement Yes Yes Java allows comma-separated expressions in the for loop initialization and increment sections, but not in the test section

Java allows the declaration of local loop variables, as in ISO C++ (but not in C or earlier versions of C++):

for (int i = 0; i < 10; i++) 
{ /* ... */ }
"foreach" statement (enhanced for loop) No No Added in Java 5.0
break statement Yes Yes Can take an optional label
continue statement Yes Yes Can take an optional label
No goto statement! Yes Yes Java has eliminated the notorious goto statement!
return statement Yes Yes  
try, throw, catch and finally statements No Yes Java supports exceptions somewhat like C++, except the exception models are rather different.
synchronized statement No No Java has thread support built into the language.

C and C++ do not

package and import statements No No Java supports modularization into packages.

C does not.

C++ has namespaces to accomplish the same thing, but they are a very recent addition to the language and so are not used by many traditional C++ programmers.

 

This page was last modified on 02 October, 2007