C++ 和 Java 的相似点和不同点

来源:互联网 发布:淘宝快递投诉怎么撤销 编辑:程序博客网 时间:2024/06/05 22:41

先讲C++和Java的相似点,在将不同点。

相似点:
1. Much of the syntax similar to C++
2. Primitive types, compund blocks, loops (all), built-in operators (most), switch, if-else
3. Static data members (静态数据)
4. Casting for primitive types (类型转换)
5. Scope of your loop control variables in a for (循环的范围)
6. Allowing definitions of a variable to occur where you need them (允许在需要的时候定义变量)
7. Use of the ++ and – operators (允许使用自增和自减运算符)
8. Break and continue (可以使用break 和 continue 运算符)
9. Two types of comments (// and /* */) 可以使用这两种的注释方式
10. Support for function overloading (unlike C) (支持函数重载, 跟C 语言不一样)
11. Static member functions (equivalent to C’s global functions)
(Java的静态成员函数 跟C语言的全局函数是一样的)
12. Global functions are not permitted in Java (Java不允许使用全局函数)
13. They don’t have a this reference
14. They can’t call a non-static member function without an object of the class
(不能使用非这个类的object来调用此类的非静态成员函数)

下面是讲不同点:
1. You cannot define the same named variable in different - inner vs outer blocks (unlike C++ allows identifiers in an inner block to hide those in an outer scope)
2. Primitive types in Java are guaranteed to have an initial value (u.e., not garbage!)
3. Java determines the size of each primitive type (they don’t change from one machine architecture to another - unlike C and C++)
(Java定义了每一个primitive type的变量的大小)
4. All numeric types are signed - they do not support the unsigned type
5. No semicolon is required at the end of a class definition.
(类的定义后面不需要有分号)
6. No const - instead we use “final” to represent memory that cannot be changed:
举个例子:
final int I = 10; //A constant value
final list object = new list(); //Reference is constant - so it can’t reference another object! However, the object itself can be modified
7. Final doesn’t require that the value of the varibale/object be known at compile time. final list obj; //says it is a “blank” final reference
8. Blank finals must be initialized in the constructor where the blank final is a member.
9. Arguments can also be “final” by placing the keyword in the argument list - which means the method cannot change the argument reference.
10. Although data members are initialized automatically (and so are arrays, variables of primitive types used in a function (i.e., local variables) are not automatically initialized (e.g., int var;).
11. You are responsible for assigning an appropriate value to your local variables.
12. If you forget, you will get an error message indicating that the variable may not be initialized.
13. Also… int are not bools in Java, you can’t use an int as part of a conditional expression like we are used to. So saying (while(x = y)) can’t happen!
[1] Because the result of the expression is not a boolean and the compiler expects a boolean and won’t convert from an int.
[2] So, unlike C++ you will get an error if you make this mistake!
14. In Java, it can only be used in for loops to allow for multiple increment steps.
15. There is no operator overloading
[1] Which means you cannot compare strings with >>= etc.
[2] You cannot assign objects to do a complete copy (=)
[3] You cannot read and write using >> or <<
16. You cannot cast class types!
[1] To convert you must use special methods (i.e., function calls)
17. But, you can assign data members values - directly :
class list{
int L = 100;
video v = new video();
}

原创粉丝点击