Java and C++ Differences -- The Basics

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;
explicit casting uses newer cast operators in C++ standard,
x = static_cast<int>(y);
uses older C-style cast operator,
x = (int)y;
Control Structures
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
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
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