Migrating from C+tto Java Java Help

Migrating from C+to Java

T’his chapter discusses several of the issues that arise when you move from C++ ,to  Java. Since happy (if not most) Java programmers come from a C++ background, it is natural to want to carryover the skills, techniques, and code acquired in this language. Although C++ and Java were designed to address the programming needs of two very different environments; many of the same coding techniques. algorithms and optimizations apply to both. However, as explained in Part One, Java is not “the Internet version of C++.” While there arc many similarities between the two languages, there arc also several differences. This chapter reviews those differences and shows ways to handle ‘most’ of the more challenging ones.

The Differences Between C++ and Java

Before we look at specific situations, let’s review the basic differences between C+-l
and java.The differences fall into three categories:
CC++ features not supported by Java
Features unique to Java
Shared features which differ between C++ and Java

Each is examined here

What- Java Has Removed from c++
There are a number of C++ features that Java does not support, In some cases, a’
specific C++ feature simply didn’t relate to the Java environmental. In other cases, the designers of Java eliminated some of the duplication of features that exists in C++. In still other instances, a feature of C++ is not supported by Java because it was deemed too dangerous for Internet applicants. Perhaps the single biggest difference between Java and C++ is that Java does not support pointers. As ax:++ programmer you know that the pointer is one of C++’s most powerful-and important language features. It is also one of Its most dangerous when used improperly. Pointers don’t exist in Java for two reasons: ” Pointers are inherently Insecure. For example, using a C++-style pointer, it Is possible to gain access to memory addresses outside a program’s code and data. A malicious program could make use of this fact to damage system, perform unauthorized accesses (such a:l obtaining passwords), or otherwise violate security restrictions.

Even if pointers could be restricted to the confines of the Java run-time system (which. is theoretically possible, since Java programs are interpreted.

Here are a few more of the most important “commissions”:
• Java does not include structures or unions. These were felt to be redundant since the class encompasses them.
• Java does not support operator overloading. Operator overloading is sometimes a source of ambiguity in a c++ program, and the Java design team felt that it causes more trouble than benefit.
• Java does not include  a processor nor does it support the processor  directives. The processor plays a less important role in C++ than it does in C. The designers of Java felt that it was time to eliminate it entirely .
• Java does not perform any automatic type conversions that result ma loss of  precision. For example, a conversion from long integer to integer must be explicitly cast.
• All the code ht a Java program is encapsulated within one or more classes. Therefore, Java does not have what you normally think of as global variables or global functions .
• Java does not allow default arguments. in C+·~(.yO~·~1~Y specify a value that a parameter will have when there is no argument corresponding to that parameter when the function is invoked. This is not allowed in  Java does not support the inheritance of multiple underclasses by a subclass. Although Java support constructors, it does not have destructs. It does. however, add the finalize ) function.
• Java does not support
• It is not possible to declare unsigned integers in Java.
• Java d~s not allow the go to.
• Java does not have the delete operator.
• The« and» in Java are not overloaded for I/O operations,
• In Java, objects are passed by reference only. In.C++,.objects may be passed by value or by reference.

New Features Added by Java

There arc a number of features in java that have no equivalent in C++. Perhaps the three most important are multi threading, packages and interfaces, but there arc
several others that enrich the Java programming environment as well.

*As explained earlier, multithreading allows two or more pieces of the same ‘program to execute concurrently. Further, this approach to concurrence is
supported at the language level. There is no parallel for this in C++. If you need to multithread a C++ program, you will need to do so manually, using operating system functions. While both methods allow for concurrent execution of two or more threads, I’Sapproach is cleaner and easier to use. Iii There is no feature in C++ that directly corresponds to a java package. The closest similarity is a set of library functions that use a common header file. However, constructing and using a library in C++ is completely different from -constructing and using a package in Java. m The Java interface is somewhat similar to a C++ abstract class. (An abstract class in C++ is a class that contains at least one pure virtual function.) For example, it is impossible to create an instance of a C++ abstract class or a Java interface. Both to specify a consistent interface that sub classes will implement. The main difference is that an interface more cleanly represents this concept. java has n streamlined approach to memory allocation. Like C++, it supports the new keyword. However, it docs not have delete. Instead, when the last reference to an object is destroyed, the object, itself, is automatically deleted the next time that garbage collection occurs. m Java “removes” the C++ standard library, replacing it with its own set of API classes. While there is substantial functional similarity, there are significant differences in the names and parameters. Also, since all of the Java API library  is object-oriented, and only a portion of the C++ library is, there will be differences in’ the way library routines are invoked.

*There is no feature in C++ that directly corresponds to a java package. The closest similarity is a set of library functions that use a common header file. However, constructing and using a library in C++ is completely different from -constructing and using a package in Java.

*The Java interface is somewhat similar to a C++ abstract class. (An abstract class in C++ is a class that contains at least one pure virtual function.) For example, it is impossible to create an instance of a C++ abstract class or a Java interface. Both Reused to specify a consistent interface that sub classes will implement. The main difference is that an interface more cleanly represents this concept.

*Java has n streamlined approach to memory allocation. Like C++, it supports the new keyword. However, it docs not have delete. Instead, when the last reference to an object is destroyed, the object, itself, is automatically deleted the next time that garbage collection occurs.

*Java “removes” the C++ standard library, replacing it with its own set of API classes. While there is substantial functional similarity, there are significant differences in the names and parameters. Also, since all of the Java API library  is object-oriented, and only a portion of the C++ library is, there will be differences in’ the way library routines are invoked.

*The break and continue statements have been enhanced in Java to accept labels
IS targets.

*The char type in- Java declares 16-bit-wide Unicode characters. This makes them similar to C++’s char type. The use of Unicode helps ensure portability.

*Java adds the »>-operator, which performs an unsigned right shift.

In addition to supporting single-line and multilingual comments, Java adds a third.
comment form: the documentation comment. Documentation comments begin
with a and end with a *l

*Java contains a built-in string type” called” String. String is somewhat similar to the standard string class type provided by C++. Of course, in CH· string is only available if you include its class declarations in your program. It is not a built-in type.

Features That Differ

There are some features common to both C++ and Java that each language handles a bit differently:

*While both C++ and lava supports Boolean data type, Java does not implement ‘  true and false in the always as C++. In C++, true is any nonzero value. Falseis zero. In Java, true and false are predefined literals, and these arc the only” values that a Boolean expression may have. While C++ also defines true and false, which may be assigned to a bool variable, C++ automatically converts nonzero values into true and zero values into false. This docs not occur in Java.

*When y u create a C++ class, the access specifiers apply to  “statements. In Java, access specifiers apply only to the declarations that they immediately precede.

*C++ supports exception handling that is fairly similar to Java’s, However, in C++ there is no requirement that a thrown exception be caught.

“With these additions, deletions, and differences as a backdrop, the rest of this . Chapter will look closely at a few of the key issues that yo~ must deal with when ‘convertible code from’Cs+ to Java.

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 18, 2014 in Migrating from C+tto Java

Share the Story

About the Author

Back to Top
Share This