C++和Java的比较

 

C++ vs. Java

The following text has been reproduced from a wonderful computer reference chart with permission from the company that produced it,BarCharts, Inc.

For C++ Programmers

Java bears a strong (intentional) relationship to the widely used C++ programming language. There are, however, a number of significant differences, the most significant of which include:

Java在很大程度上与C++编程语言有很大的关系。然而,还是有以下一些明显的区别:

Data Types: In C/C++ many data types (such as int) are hardware dependent. In Java, the size and manner of representation of data is specified, to facilitate machine independence of code.

数据类型:C/C++中数据类型是硬件相关的。而在Java中,数据类型的大小时与硬件无关的,即所有硬件平台都是同样的大小。

References vs. Pointers: In C/C++, variables that contain addresses of data and functions,pointers, can be declared. In Java, no addresses are used. Instead,references are used to establish variable names for objects.

引用和指针:C/C++中,数据、函数、指针在定义的时候会被分配地址。而在Java中,不使用地址,而是引用来声明所有对象的变量名。

Automatic Variables: In C/C++, memory for variables declared within functions (i.e.,local variables) will normally be allocated automatically, on the stack. InJava, all memory for non-intrinsic data objects must be acquired with thenew operator.

自动变量:C/C++中,函数内的局部变量通常在栈上自动分配。而在Java中,所有的非基本类型数据必须采用new操作符来申请内存(堆上)。

Dynamic Memory Management: In C++, memory is dynamically assigned by the programmer (using thenew operator) and must also explicitly be freed (using thedelete operator). In Java, a garbage collection process automatically locates the memory associated with any object that is no longer referenced within the program, meaning no explicit deletion is required.

动态内存管理:在C++中,内存是由程序员通过new操作符动态申请,并且通过delete操作符显式的释放。而在Java中,垃圾回收进程将自动地释放哪些不再使用的引用指向的对象占用的内存,不需要显式的删除对象。

注:一些清理工作还是需要程序员自己定义特殊的方法来实现。

Global Data and Functions: In C++, data and functions can be declared outside of any class definition. (In C, all functions are declared that way). InJava, all data and functions must be declared within class definitions. Like C++, however, astatic declaration may be used to make member data/functions object dependent.

全局数据和函数:在C++中,数据和函数可以被定义在任何类的外部(C中更是如此)。在Java中,所有的数据和函数必须定义在类内部。和C++一样,一个static的定义可以用来将成员数据和函数对象独立出来而与类对象无关,仅与类相关。

Inheritance: Unlike C++, Java permits only one parent per class. Many benefits of multiple inheritance be achieved, however, usingJavaInterfaces. Java class constructor functions can also explicitly invoke parent constructors with thesuper keyword.

继承:和C++不同,Java一个子类仅允许有一个父类(即无多重继承)。多重继承的好处可以通过使用Java接口来实现。Java类的构造函数可以显式的通过super关键字调用父类的构造函数。

Boolean Expressions: In C/C++, boolean (false/true) expressions translate to 0, nonzero integers. InJava, test are done with the boolean data type. As a result, expressions containing && and || stop evaluating an expression once its results are known to betrue or false, referred to as a shortcircuit. The & and | operators, in contrast, evaluate both sides of their expressions even if the final result is already known (e.g., the left side of an & expression is false ).

布尔表达式:在C/C++中,布尔(false/true)表达式可以转译为0,非0整数。在Java中,只允许boolean数据类型来测试布尔值,不能通过整数转换为boolean值(即不存在 if(10) 这样的表达式作为布尔表达式).与C/C++中一样,一旦在一个包含&&和||的布尔表达式中提前得到ture或false值,表达式将被短路(即不再评估后面的结果).&和|操作符则相反,将评估表达式两边的结果即使最终的结果是已知的(左侧&表达式是false)。

注: & | 其实是按位操作符,而非严格的逻辑操作符,也就是按位操作以后作为逻辑判断。

The information above has been reproduced with permission from BarCharts, Inc.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值