CS253: C++ traps for the unwary Java programmer
This is not a list of all Java/C++ differences. Instead, it is a list of differences that are likely to cause problems for a programmer who knows Java but is learning C++.
If you ask, “Why doesn’t C++ do such-and-such a thing like Java?”, then you’re asking the wrong question.
In Java, everything’s a method, because everything’s in a class.
C++ has methods inside of classes. However, C++ also has
functions outside of classes, like main()
.
void main(String[] args)
int main()
int main(int argc, char *argv[])
In C++, main
returns an int indicating success/failure.
Zero indicates success, positive numbers indicate failure.
It’s not a boolean success indicator. It’s an integer code.
In C++, main
uses an array of old-style C char *
strings,
for compatibility with C.
boolean
bool
char
, byte
char
C++’s char
type is the closest approximation to Java’s byte
.
Java’s char
can hold Unicode characters.
C++’s char
has an implementation-defined size, but it’s typically
a single byte.
final
final
, const
, constexpr
In Java, final
indicates a non-overrideable method,
or a constant value.
In C++, final
indicates a non-overridable method.
const
indicates a method that doesn’t alter object state,
or a value that you can’t change.
constexpr
indicates a compile-time constant.
int[] a = new int[100];
int a[100];
In C++, arrays are not objects. Hence, they have no methods.
Hence, you can’t ask an array how long it is. Use a vector
or std::array
instead, if you want that.
The simple term “array” is, alas, ambiguous. We will use the
phrases “C-style array” or std::array
to resolve this.
String
string
In Java, classes start with a capital letter. That’s not always so in C++.
String s = new String;
string s;
In Java, objects are always in the heap—dynamic memory. In C++, they can be, but don’t have to be.
char c = s.charAt(3);
char c = s[3];
Sure, C++’s string
class has an .at()
method, but, why?
int n=0; int v = ++n + ++n;
int n=0; int v = ++n + ++n;
The Java example yields 3
, because the order of operations
is defined by the language.
The C++ example invokes undefined behavior. The compiler is free
to do those operations (++
, ++
, +
) in whatever order
it considers to be best.
"foobar"+3
"foobar"+3
In Java, +
is overloaded to handle arguments of String
and int
,
and so yields "foobar3"
.
C++ performs address arithmetic in this case, and so yields "bar"
.
Java has pointers, which it calls “references”.
C++ has explicit pointers, and references like Java’s.
alpha.beta()
alpha.beta()
or alpha->beta()
In Java, alpha
can only be a reference.
In C++, alpha
might be an object, a reference to an object,
or a pointer to an object.
delete
obj1 = obj2;
obj1 = obj2;
In Java, this copies a reference. No new object is created, no real data is copied.
In C++, the copy ctor of the class is called. This typically copies the data in the object.
a[-12]
a[-12]
Java throws an exception.
C++ assumes that you know what you’re doing.
null
nullptr
or NULL
or 0
In Java, null
is the reference to nothing.
In C++, a pointer that points to nothing useful can be initialized
to nullptr
or NULL
or 0
.
nullptr
is best
NULL
is compatible with C
0
is just plain stupid
Modified: 2017-01-08T13:01 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |