Java Cookbook

Porting C++ to Java

by Mark Davis


Introduction, Basics, Next Steps, Well-Mannered Objects, Esoterica, Background, Index


Last Update: March 25, 1997

Acrobat Version

Java Cookbook News Group


* Introduction

Java has taken the programming world by storm. In our own experience in developing software at Taligent, its turnaround time is several times faster than that of C++, a crucial feature in today's markets. Moreover, it has significant advantages over C++ in terms of portability, power, and simplicity. These advantages are leading many people to consider replacing C++ with Java, not only for web applets, but increasingly for client and server applications as well.

Yet there is a large amount of code already written in C++. For just this reason, many people are considering porting their existing C++ code to Java. This paper is addresses those people, providing a step-by-step approach to porting C++ to Java effectively, with special attention to the following:

  • *Pitfalls. In most cases, the differences between the languages are syntactic, and the compiler will discover whether you forget to make a change. However, there are a few instances where the same code in C++ and Java has dangerously different consequences. These pitfalls are marked with the graphic on the right (Unicode character 2620).
     
  • Minimal effort. We assume that you are not, at this point, interested in completely revamping your code, even though it may be old crufty stuff that you inherited from someone else. As much as possible, therefore, we minimize your work by providing techniques for a one-for-one match between your old code and new.

This paper is not specifically directed at the Java beginner, although it can be useful for those getting started. There are many books available for learning about Java and object-oriented program design; for some of our favorites, see References.

The following sections are covered in this introduction.


* If it ain't wrong...

Since to a large degree Java follows C++ conventions, the bulk of your code will remain unchanged: variable names, flow of control, names of primitive types, and so on. As you do in C++, in Java you will write classes, override methods, overload methods, write constructors, instantiate objects, and so on. The following elements of the two languages are very close and generally need little modification.

  • Primitives
    • char, int, short, long, float, double, void (but not void*)
    • variable names
  • Flow of Control
    • if, else, for, while, do, switch, case, default, break, continue, return, static
  • Operators
    • +, -, !, %, ^, &, *, |, ~, /, >, <, (), [], {}, ?:, ., =, ++, --, ==, <=, >=, !=, >>, <<, ||, &&, *=
  • Comments
    • /*...*/, //

For example, these snippets of code remain unaltered when ported from C++ to Java.

Bulk of Code Unchanged

C++

Java

// some sample lines of code

int x = 3;

for (int i = 0; i < j; ++i) {
  x += i * i;
}

x = y.method1(3,4);
// some sample lines of code

int x = 3;

for (int i = 0; i < j; ++i) {
  x += i * i;
}

x = y.method1(3,4);

When porting from C++ to Java, your job is far easier than when porting to Basic, FORTRAN, Lisp, Smalltalk or other radically different languages. Keep that in mind as you go through the remainder of this document: the items I list are the exceptions, not the rules.


* The compiler is your friend

The Java compiler is much more rigorous than that of C++, so much of the code that needs to be changed will be found by the compiler. With each description of a porting task, examples will show you what you have to do to C++ code to change it to Java code. In these examples, corresponding lines of code in C++ and Java are lined up (although in some older browsers this doesn't work properly). The text is also color-coded, in the following fashion.

 Color

Meaning

 Red Pitfall (aka faux amis): code that looks the same, but has quite a different meaning!
 Blue Changes between C++ and Java code.
 Black Code that is the same in Java as it is in C++.
 Brown Items with special comments in the notes below.
 Green Comments.

For brevity, code snippets include only enough of the context to be understandable!

In discussing Java programming there is also some terminology that we find useful, especially for discussing how to deal with references and the lack of const.

Immutables
Those classes or objects whose state can't be changed, such as String, Number, or Locale. Typically this means that there are getters (a.k.a. accessors), but no setters (a.k.a. mutators). The entire state of the object is determined by the constructor.
Mutables
Those classes or objects that are not Immutable. This also excludes primitives (boolean, byte, char, short, int, long, float, double).
Sliced
An object that has been converted to a superclass object, with loss of data.


* Getting down to business

This article is divided up into the web pages below. (It is not split up further, since it is pretty annoying to print very many pages with today's browsers.) It is also organized to be useful when printed, with some caveats.[4]

On each page in this article, there are links to the sections on that page, plus links to the top of the page (that look like ). There are also the occasional footnotes, which are indicated in the text with a superscripted number in brackets[3] and found at the very end of the article.

  1. Introduction
  2. Basics
    The following sections walk you through the main steps necessary to convert your program from C++ to Java.
  3. Next Steps
    The following sections take you further through the steps necessary to convert your program from C++ to Java.
  4. Well-Mannered Objects
    The following sections describe particular issues that are common to almost all classes, but are often tricky to get right.
  5. Esoterica
    The following sections deal with somewhat less common constructs in C++.
  6. Background Information
    The following sections provide background information, plus an index to topics.

*

Introduction, Basics, Next Steps, Well-Mannered Objects, Esoterica, Background, Index




JavaTM is a trademark of Sun Microsystems, Inc.

Other companies, products, and service names may be trademarks or service marks of others.

Copyright    Trademark



  Java Education Java Home  
IBM HomeOrderEmployment