woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

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

原文:http://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html
首先,两件大事-主函数和怎样编译,下面是它们的小差别:

主函数

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 );

    }

   

 

posted on 2009-05-02 01:43 肥仔 阅读(1201) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理