您的位置:首页 > 编程语言 > C语言/C++

C++和Java的语法对比手册

2013-07-25 18:05 309 查看
http://www.eit.name/blog/read.php?474

[

2010/12/05 22:20 | by
ipaddr ]

首先,两件大事-主函数和怎样编译,下面是它们的小差别:

主函数

C++

// 自由函数

int main( int argc, char* argv[])

{

printf( "Hello, world" );

}



Java

// 每个函数必须是类成员;当java类运行时类中的主函数就会被调用

//(所以你可以为每个类写一个主函数--这样用于给类写单元测试时会很方便)

class HelloWorld

{

public static void main(String args[])

{

System.out.println( "Hello, World" );

}

}



编译

C++

// 编译

g++ foo.cc -o outfile

// 运行

./outfile



Java

// 编译在foo.java里的类成为<类名>.class

javac foo.java

// 调用<类名>中的静态main函数

java <classname>



注释

两种语言完全相同 (// 和 /* */ 都能工作)

类声明

大部分一样,除了Java不要求后面有个分号

C++

class Bar {};



Java

class Bar {}



方法声明

基本相同,除了Java中必须是类成员并且可能有public/private/protected前缀之外。

构造和析构

构造语法相同,Java没有析构的等价物。

静态成员函数和变量

方法声明方式相同,不过Java提供了static initialization blocks 来初始化静态变量(代替在源码文件中放定义):

class Foo

{

static private int x;

// static initialization block

{ x = 5; }

}

对象声明

C++

// 栈对象

myClass x;

// 堆对象

myClass *x = new myClass;



Java

// 总是分配在堆上(而且,构造总是要写括号)

myClass x = new myClass();



引用vs.指针

C++

// 引用是不可改的,使用指针能得到更大的弹性

int bar = 7, qux = 6;

int& foo = bar;



Java

// 引用是可改的,它仅存放对象的地址。没有原生指针。

myClass x;

x.foo(); // 错误,x是空“指针”

// 注意Java里总是使用 . 存取域



继承

C++

class Foo : public Bar

{ ... };



Java

class Foo extends Bar

{ ... }



保护级别

C++

public:

void foo();

void bar();



Java

public void foo();

public void bar();



虚函数

C++

virtual int foo();



Java

// 函数默认就是虚函数;使用final防止被重载

int foo();



抽象类

C++

// 只要包含一个纯虚函数

class Bar { public: virtual void foo() = 0; };



Java

// 可以用语法直接定义

abstract class Bar { public abstract void foo(); }

// 或者指定为接口

interface Bar { public void foo(); }

// 然后,用一个类实现implement

它:

class Chocolate implements Bar

{

public void foo() { /* do something */ }

}



内存管理

大致相同--new 分配, 不过因为Java著名的垃圾回收机制所以没有delete 。

NULL vs. null

C++

// 初始化指针为NULL

int *x = NULL;



Java

// 使用未初始化的引用会被计算机捕获,不过可以赋值为null指明引用为无效。

myClass x = null;



布尔值

Java要长一点,你得写boolean来代替简短的bool。

C++

bool foo;

Java

boolean foo;

常量

C++

const int x = 7;



Java

final int x = 7;



Throw说明

首先, Java在编译时强制要求有throw说明-如果一个方法要抛出一个异常你必须先说明它

C++

int foo() throw (IOException)

Java

int foo() throws IOException

数组

C++

int x[10];

// 或者

int *x = new x[10];

// 使用x然后收回内存

delete[] x;



Java

int[] x = new int[10];

// 使用x,内存由垃圾回收机制回收



集合和迭代

C++

迭代器是类成员,一个范围起始于<container>.begin(), 终止于<container>.end(). 使用++操作前进,使用*存取。

vector myVec;

for ( vector<int>::iterator itr = myVec.begin();

itr != myVec.end();

++itr )

{

cout << *itr;

}



Java

迭代器仅仅是一个接口。一个范围起始于<collection>.iterator, 接着使用itr.hasNext()检查确认是否已到末尾。使用itr.next()取得下一个数据。

ArrayList myArrayList = new ArrayList();

Iterator itr = myArrayList.iterator();

while ( itr.hasNext() )

{

System.out.println( itr.next() );

}

// 在Java 5中:

ArrayList myArrayList = new ArrayList();

for( Object o : myArrayList ) {

System.out.println( o );

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: