![]() |
![]() |
![]() |
![]() |
Java Cookbook: Porting C++ to JavaBasicsIntroduction, Basics, Next Steps, Well-Mannered Objects, Esoterica, Background, Index The following sections take you through the main steps necessary to convert your program from C++ to Java.
|
C++ |
inlined C++ |
Java |
---|---|---|
// file.h class Foo :: Bar { public: int square(); int cube(); private: int x; } // file.c (or .cpp) int Foo::square() { return x*x; } int Foo::cube() { return x*x*x; } |
// file.h class Foo :: Bar { public: int square() { return x*x}; int cube() { return x*x*x; } private: int x; } |
// file.java package com.xyz.abc; import com.xyz.abc.*; import com.xyz.def.*; class Foo extends Bar { public int square() { return x*x}; public int cube() { return x*x*x; } private int x; }
|
friend
as in C++. See
Java has no friends for
more information about how to handle this.
Next, you need to change a few names. There are a few cases where there is a relatively straightforward name change.
C++ |
Java |
---|---|
bool x = true; |
boolean x = true; |
Most name differences, however, depend on the context.
C++ |
Java |
---|---|
// const field static const int x = 3; // const method int doSomething() const; // character data char ch = 'b'; // byte data (e.g. short numbers) char b = 31; // abstract method int someMethod() = 0; // non-virtual method int someMethod(); // virtual method virtual int someMethod(); // unknown object (no primative) void* doAnother() {} |
// const field static final int x = 3; // const method int doSomething(); // character data char ch = 'b'; // byte data byte b = 31; // abstract method abstract int someMethod(); // non-virtual method final int someMethod(); // virtual method int someMethod(); // unknown object Object doAnother() {} |
Const
, in particular, requires very special handling,
and is discussed in detail in Bullet-proofing.
The simplest approach at the start is to change it
to final for any field, and remove it otherwise.
char
as a small number or as a piece of character data; in general, though,
it usually corresponds to character data and can be left alone. It is also
discussed at more length below.
final
in front of every method that doesn't contain
the word virtual
, then delete all instances of virtual
.
There is one complication; in C++, if a method is marked virtual
in a superclass, then it is implicitly virtual in all subclasses.
So, you may need to look at superclasses to see if the method is really
virtual.
Java does not support operator overloading. You will miss this for about 5 minutes if you are programming in pure Java, but it is a hassle when converting from C++. First you will need to change all the definitions.
Here is a sample list of operators that could be overloaded, and some
typical Java equivalent names (there is no fixed set of replacement names;
these are only samples.) If you are porting good C++, then the meaning of
the operator does not deviate from the core meaning; if not, then you should
change the name to correspond to the real meaning (such as append
for +). The yellow items have special notes.
|
|
x%y
to (x%y - ((x < 0) ? y : 0))
Once you have changed all of the definitions, let the compiler find the call-sites for you to fix.
C++ |
Java |
---|---|
// declaring bool operator== (const Foo& other) const; // using if (a == b) a[3] = 5; x = a[3]; |
// declaring public boolean equals(Foo other) { /*...*/ } // using if (a.equals(b)) a.setElementAt(3,5); x = elementAt(3); |
Java is touted as having no pointers. In porting from C++ code, however, you almost want to think of it as the reverse; all objects are pointers--there are no stack objects or value parameters. The syntax of the language hides this fact from you, but you have to be careful, as the following examples show.
C++ |
Java |
---|---|
// initializing Foo* x = new Foo(3); Foo y(4); Foo z; // assigning Foo* a = x; Foo* c = 0; Foo* d = NULL; Foo b = y; // calling x->doSomething(); y.doSomething(); // comparing if (x == a); if (y == b); if (&y == &b); |
// initializing Foo x = new Foo(3); Foo y = new Foo(4); Foo z = new Foo(); // assigning Foo a = x; Foo c = null; Foo d = null; Foo b = y.clone(); // calling x.doSomething(); y.doSomething(); // comparing if (x == a); if (y.equals(b)); if (y == b); |
clone()
to get a new object.
equals()
to get comparision by value.
Java also does not have references in the same way as C++, although they use the term references for normal objects (much like the conflation of objects and pointer to objects). Most C++ programs only use them in passing parameters to a method, or getting a return value back.
C++ |
Java |
---|---|
// input parameter int method1(const Foo& x); // Mutable output parameter int method2(Foo x); // Immutable output parameter int method2(int& x); // usage Foo x; z = y.method2(x); w = x; |
// input parameter int method1(Foo x); // Mutable output parameter int method2(Foo x); // Immutable output parameter int method2(int[] x); // usage Foo[] x = new Foo[1]; z = y.method2(x); w = x[1]; |
C++ |
Java |
---|---|
// output parameter int method2(int& x); // usage int x; z = y.method2(x); w = x; |
// output parameter int method2(IntWrapper x); // usage IntWrapper x = new IntWrapper(); z = y.method2(x); w = x.intField; // new class class IntWrapper { public Int intField; } |
Return values can also be references. There are two common idioms for reference returns in C++.
*this
. This method allows chaining, as in x
= y = z;
method2
fills in x
, then returns it for
further use. Since the principal use of this is in handling memory allocation,
there is little need for it in Java, but it may make your porting easier
to leave it as is.
All of these idioms can be used in Java, though the compiler may warn you of problems if you are trying to set a Mutable. In that case, you will have to supply some of the same techniques as with output parameters.
C++ |
Java |
---|---|
// definition Foo& setX(); // return of ouput parameter Foo& getY(Foo& Y); // return static constant const Foo& getAStatic(); // use myObject.setX(3).setY(4); Foo x; myObject.doZ(myObject.getY(x)); z = x * Foo::getAStatic(); |
// definition Foo setX(); // return of output parameter Foo getY(Foo Y); // return static Foo getAStatic(); // use myObject.setX(3).setY(4); Foo x = new Foo(); myObject.doZ(myObject.getY(x)); z = x * Foo.getAStatic(); |
&myCharArray[5]
. Although legal, this
can be the source of many nasty headaches, since the results are undefined
if the enclosing storage is altered or moved.
Java arrays are real objects, not just disguised pointers. Generally you replace pointers used to iterate through an array by offsets, and the * operator by an array access. Most of these cases will be flagged by the compiler.
C++ |
Java |
---|---|
// initializing double x[10]; double* end = x + 10; double* current = x; // iterating while (current < end) { doSomethingTo(*current++); } |
// initializing double[] x = new double[10]; int end = x.length; int current = 0; // iterating while (current < end) { doSomethingTo(x[current++]); } |
Foo x[]
and Foo[] x
work,
though the latter is more Java-like.
Declaring an array does not create object to fill an array. This is another place where objects behave like pointers, not values. Since arrays of objects are--under the covers--arrays of pointers, they are initialized to null; not to a list of default-constructed objects. If you want them to be default-constructed objects, you must set them yourself!
One other point: although the allocation of static arrays looks similar to C++, be aware that there are vastly different footprint and performance implications. Unlike C++, which builds a static table that is loaded in at runtime, the Java compiler actually generates code to place every single entry in the array, so a very large table could be a performance and footprint hit.
C++ |
Java |
---|---|
// initializing Foo x[10]; // initializing static const int x[] = {1,2,3,4,5,6,7,8,... |
// initializing Foo[] x = new Foo[10]; for (int i = 0; i < x.length; ++i) { x[i] = new Foo(); } // initializing static const int x[] = {1,2,3,4,5,6,7,8,... |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
||||
Products | Object Resources | In the News | Company Info | Search |
If you encounter problems with this service please contact
webmaster@taligent.com.
© Copyright. All rights reserved. Taligent, Inc., IBM Corp. |
||||