Table of Contents
Statements in Java are similar, but not identical, to C/C++.
The Java Statements
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 sectionJava 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. |
Differences from C/C++
Here are some other ways that Java statements differ from C/C++ statements:
| Feature | In C? | In C++? | Comments |
|---|---|---|---|
| Local variable declarations | No | Yes | Java supports C++-style local variable declarations. (C only supports local declarations are the start of a block.) |
| Forward references | Yes | Yes | Java supports flexible forward references.It does not have the concept of declarations and definitions as in C/C++. |
| Method overloading | No | Yes | Java uses overloading similar to C++.Java does not support global functions. |
| The void return type | Yes | Yes | Java uses void for a return type, but does not support (void) casts |
| The void argument list | Yes | Optional | Java does not use void to indicate no parameters |
| The void * type | Yes | Yes | Java has no pointer types |
| Old modifiers | Yes | Yes | Java does not support long, short, signed, unsigned, or const as modifiers.Java does support the volatile modifier. |
| New modifiers | No | No | Java supports modifiers: final, native, synchronized, and transient |
No structs or unions | Yes | Yes | Java does not support structs or unions |
| Enumerations (enums) | Yes | Yes | Java did not support enums prior to JDK 1.5 (Java 5). As of Java 5, it now does. |
| No method types | Yes | Yes | Java does not support function/method addresses |
| No bitfields | Yes | Yes | Java does not support bitfields |
| Variable-length argument lists | Yes | Yes | Java did not support variable-length argument lists. However, in JDK 1.5 (Java 5), it does support variable-length argument lists. |
The “foreach” Loop
Java 5.0 added a very useful, new version of a for loop: a “foreach” loop, which is also present in a number of other languages. Java calls this new version an “enhanced for loop”.
The “foreach” loop allows you to iterate through a collection of objects in a way that is much simpler than with a normal for loop.
For the moment, we have only encountered one kind of collection: an array. Later, we’ll see how the “foreach” loop may be used with other kinds of collections.
Pre-Java Version 5
First, let’s look at how we typically would have done this prior to Java 5.0:
package examples;
public class ForEachTest
{
public static void main(String[] args)
{
String[] herbs = { "Basil", "Sage", "Rosemary", "Oregano" };
for (int i = 0; i < herbs.length; i++)
{
String herb = herbs[i];
System.out.println(herb);
}
}
}
which outputs:
Basil Sage Rosemary Oregano
This form of a for loop uses a very standard Java idiom (it’s also a standard idiom for C/C++ and other related languages) for iterating through the elements of an array. Note how picky you have to be with the termination condition:
i < herbs.length
“Off by one” errors (also known as “fencepost” errors) are very common, even for experienced Java programmers.
Post-Java Version 5
Now, let’s see how the “foreach” loop makes this much simpler (and safer!):
package examples;
public class ForEachTest
{
public static void main(String[] args)
{
String[] herbs = { "Basil", "Sage", "Rosemary", "Oregano" };
for (String herb: herbs)
{
System.out.println(herb);
}
}
}
which outputs exactly the same thing as the former example.
Notice how much simpler this version is! Not only does it save several lines of code, it also eliminates the error-prone termination condition of the conventional for loop construct.
The compiler is doing more work for you!
