Item | C++ | Java |
---|---|---|
Basic Syntax Building Blocks | ||
built-in types | bool | boolean |
char for the usual single-byte type | char - 2 bytes (unicode) byte - 1 byte (numeric) |
|
no long double type | ||
exact sizes of basic types are system-dependent | sizes for each type are fixed | |
main function | int main() | public static void main(String[] args) |
standalone function | method (member function) of a class, Any class can have a main method |
|
methods | functions can be stand-alone | all methods (functions) are members of a class |
identifiers | consist of letters, digits, underscores | same, but can also have (and start with) the dollar sign $ |
declaring constants | uses keyword const, const int SIZE = 10; |
uses keyword final, final int SIZE = 10; |
initializing variables | Using a variable in expression without initializing can cause logic errors | Using a variable in a (read) expression without initializing first results in compile error |
explicit casting | uses newer cast operators in C++ standard, x = static_cast<int>(y); |
uses older C-style cast operator, x = (int)y; |
implicit conversions | bool and char types are compatible with some integer types Smaller types to larger types is typically the rule |
No automatic conversions between boolean and integer types No automatic conversions from integer types to char No automatic conversions from char to short or byte char to int and higher is okay |
Control Structures and logical expressions | ||
test expressions | if (expression) expression can be anything non-void. 0 = false, anything else = true. Also applies to loop test expressions |
if (boolean expression) must be a boolean expression (true/false only) -- else compile error. Also applies to loop test expressions |
switch | Java switch statement works just like in C++.
Types allowed are integer, String, and enumerations switch expressions added in Java 14 as a newer standard feature -- includes some updated syntax that allows a switch block to return a value and dispense with break statements. Possible examples later in course. |
|
Bitwise (logical) operators | Bitwise operations for use on integer operands:
|
In Java, these operators also exist, but they have two different uses:
|
Bitwise right shift | operator >> does right shift with integer operands. Left side "fill" may be system dependent (could be 0-fill or sign-fill) | The operator >> does right shift with a sign-fill. The operator >>> does right shift with a 0-fill. |
Methods | ||
Modifiers | modifiers like public, private are labels | function modifiers (including public, private, protected) go ON the functions, before the return type |
Parameter passing | Pass by Value Pass by Reference Pass by Address |
In Java, there is only Pass By Value syntax |
Method calls | Functions can be stand-alone (called by name) Functions can be methods (member functions) -- called with dot-operator from outside a class |
All functions are methods (member functions) of a class If inside the class, can call directly by name From outside a class, call with dot-operator |
Default parameter values | C++ can have default value on parameters void Func(int x, int y = 4); |
No default values on parameters |