Java 对比 C++ for programmers

C++Java
assignment operator= cannot be user-defined for a class and performs assignment of a reference to the instance of the class (see also reference types)
basic_string String and StringBuffer
boolboolean
charbyte
const variables/data members final variables/fields
copy constructorno default; one implements the interface Cloneable by the method Object clone(), which can be an abstract (in C++ notion: virtual) method
data membersfields, so-called instance variables (a term borrowed from Smalltalk)
deletedoes not exist; all unreferenced memory is garbage collected
derived classessubclasses; the keyword extends replaces C++'s colon.
destructors ~Class protected void finalize(); note, however, that these are used for freeing resources other than memory and are therefore rarely needed
exceptions, try, catch, throw, std:exception same concept; Java adds a keyword throws that is used to declare the exceptions a method throws; the hierarchy of exceptions is rooted in java.lang.Exception; a finally block is introduced to contain all common clean-up code.
extern "C" functions native methods
functionsdo not exist; static methods (``class methods'') are used
#includedoes not exist; the paths to the files are known and can be made know in the CLASSPATH environment variable
input/output: istream& operator», ostream& operator« System.in and System.out are the streams; Java has number formatting tools in java. lang. Number and java. text. Format. NumberFormat
main(int argc, char* argv[]) public static void main(String [] args) within a public class
member functionsmethods
multiple inheritancedoes not exist; however, interfaces provide a weak form of multiple inheritance.
namespacespackages
namespace Namespace{...} packagePackage; which must appear as the first line in the file
nested (member, inner) classesJava 1.1 has static (``top-level'') and non-static (``member'') inner classes, as well as local classes and anonymous classes. Member classes can refer to the members of the outer class and to OuterClass.this; they cannot have the name of an outer class and cannot declare static members.
new Class(...) new Class(...), which returns a reference to the created object
NULL (the 0 pointer value) and the type void* null in Java is a keyword and represents an uninitialized reference
overloaded operatorsdo not exist; however, methods can be overloaded. This may be a major shortcoming of Java, as one cannot revise old Java code by redefining the operators used (cf. MITMatlab)
passing arguments to base class constructorplace the statement super(...); as the first statement in the subclass's constructor
public, private, protected modifierssimilar as in C++; visibility of classes and nested classes can be also restricted; there are no friends, but within the same package protected members are visible
purely virtual member functions abstract methods; the enclosing class must also be declared abstract
reference types Type& all Java types except scalar primitive types are reference types; note that the method
void swap(T a, Tb) {T t; t = a; a = b; b = t;}
does nothing to its arguments.
scope resolution, operator :: does not exist; methods must be defined inside the class declaration. If a base class field is to be explicitly referred, one uses typecasting: ((Baseclass)Variable).Member; a direct base class member can be referred to by super.Member; typecasting has no effect on methods (see virtual member functions).
static data members static fields, so-called class variables; they are accessed by Class.Field rather than the C++ Variable.Member; they can be initialized by =...; within the class definition and need not be declared outside like C++ static data members.
static member functions static methods, so-called class methods; they are defined within the class declaration, unlike in C++.
this this, which is a reference to the object and has the type of the class, not a pointer; note that the call this(...); as the first statement in a constructor invokes a constructor call for the matching argument types.
traitsmarker interfaces
type_id instanceof; this is an operator returning a boolean, not a ``type_info'' as in C++.
using namespace Package; import Package.*;
virtual member functionsin Java, all methods use dynamic method lookup and therefore are be default virtual. There is no way to explicity call an overridden base class method, but overwriting can be prevented by declaring a method final.
wchar_t char
wide character stream wostream PrintWriter replaces PrintStream that cannot hold unicode; the constructor of PrintStream has been deprecated in Java 1.1, but System.out is not.

 

Java concepts missing in C++
abstract windows toolkit AWTstandard library for building a GUI
concatenation of strings by + operator
documentation commentscan be processed (e.g., by javadoc) for automatic online documentation
final methodsthose cannot be overridden by a subclass
interfacesare used to denote abstract classes without any method of their own. They can have static final fields. One class can implement several interfaces, but it must implement the abstract methods of each interface.
reflectionallows the inspection of a class (which arguments does which member take? etc.); this is critical for plug-and-play design, such as a Java bean
right shift operator with zero extension >>>
serializationC++ requires the programmer to implement object serialization member functions
sockets
threads

 

C++ concepts missing in Java
const member functionsdo not exist; final methods cannot be overridden by subclasses
friend classes, functionsdo not exist; however, protected members are visible within the same package
gotois a reserved work in Java, but is not supported by the language; however break and continue statements can give a statement label
multiple inheritance virtual base classes seem unachievable by using interfaces
new(Pointer) Type(...); Pointer->~Type(); this is C++'s explicit memory allocation mechanism. In Java, all memory is managed by the VM and garbage collection is automatic. Thus, in C++, a garbage collector can be implemented, while in Java a memory manager cannot.¶
pointer types Type* do not exist; actually, since Java has only reference types, all variables are some kind of pointers and the = operator behaves like a pointer assignment
pointer to function, membernot a serious restriction, as one may encapsulate a function in a function object
standard template library STL java.util.Vector provides an expandable vector. Java 1.2 provides Collections, which are essentially C++ STL containers, but many of the members are renamed. Note that List is a scrollable list in the AWT. There are third-party vendor container packages: See http://reality.sgi.com/ austern_mti/java/index.html, http://www.objectspace. com/developers/jgl/downloads/index.html§
templatesthere is a the GJ compiler http://www.cs.bell-labs. com/~wadler/pizza/gj/.§ C++'s template expansion mechanism is a full-fledged programming language and has been used for compiler optimization task (e.g., in the Blitz++ matrix library)
typedefasside as a shorthand, typedefs can be encapsulated in a class scope to provide a generic type; they function as assignments in template meta-programming.
¶Laurent Bernardin points out that this isn't exactly true: place all objects on arrays/lists for reuse
§These references were provided by Thierry Gautier
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description Written for the moderately experienced Java programmer, this book builds on readers¿ existing knowledge of object-oriented programming and covers all important aspects of Standard C++—emphasizing more lower-level C-style details later in the presentation. Chapter topics include philosophy of C++, simplest C++, pointers and reference variables, object-based programming: classes, operator overloading, object-oriented programming: inheritance, templates, abnormal control flow, input and output, collections: the standard template library, primitive arrays and strings, C-style C++, and using Java and C++: the JNI. For new C++ programmers converted from Java. For experienced Java programmers and students who require the skills of C++ programming, best-selling author Mark Allen Weiss bridges the gap. He efficiently presents the complex C++ language in this well-designed tutorial/reference that both students and seasoned programmers will appreciate. The book is ideal as a primary text for intermediate C++ courses, as a supplemental no-nonsense reference for other courses, or for independent learning by professionals. C++ for Java Programmers is a concise, well-written text that provides authoritative and up-to-date coverage of key features and details of C++, with a special focus on how C++ compares to Java. The book's approach shows knowledgeable students or professionals how to grasp the complexities of C++ and harness its power by mutually addressing the benefits and the pitfalls of the two languages. By highlighting the features and comparative elements of each language, and building on the reader's existing knowledge of object-oriented programming, C++ for Java Programmers enables users to master the essentials of C++ quickly and thoroughly. Key Features Includes insightful comparisons of the two programming languages throughout the text and points out the subtleties of C++ Succinctly covers the pertinent highlights of STL (Standard Template Library) and the most effective use of templates Explains the use of the powerful JNI (Java Native Interface) for combining Java and C++ Includes a summary of key C++ features and issues with each chapter Provides extensive treatment of C details the programmer is likely to encounter in C++ Companion Website for complete online source code at: http://www.prenhall.com/weiss Available Instructors Resource CD-ROM Product Details Paperback: 304 pages Publisher: Prentice Hall; 1 edition (November 7, 2003) Language: English ISBN-10: 013919424X ISBN-13: 978-0139194245 Product Dimensions: 9.5 x 6.8 x 0.6 inches

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值