前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++、Java语法差异对照表

C++、Java语法差异对照表

作者头像
Enjoy233
发布2019-03-05 14:44:09
发布2019-03-05 14:44:09
1.7K00
代码可运行
举报
运行总次数:0
代码可运行

C++、Java语法差异对照表

C++ and Java Syntax Differences Cheat Sheet

First, two big things--the main function and how to compile it, followed by lots of little differences.

main function  主函数

C++

代码语言:javascript
代码运行次数:0
运行
复制
// free-floating function
int main( int argc, char* argv[])
{
    printf( "Hello, world" );
}

Java

代码语言:javascript
代码运行次数:0
运行
复制
// every function must be part of a class; the main function for a particular
// class file is invoked when java <class> is run (so you can have one
// main function per class--useful for writing unit tests for a class)
class HelloWorld
{
    public static void main(String args[])
    {
        System.out.println( "Hello, World" );
    }
}

Compiling编译与执行过程

C++

代码语言:javascript
代码运行次数:0
运行
复制
    // compile as
    g++ foo.cc -o outfile
    // run with
    ./outfile

Java

代码语言:javascript
代码运行次数:0
运行
复制
    // compile classes in foo.java to <classname>.class
    javac foo.java 

    // run by invoking static main method in <classname>
    java <classname>

Comments 注释

Same in both languages (// and /* */ both work)

Class declarations 类的声明

Almost the same, but Java does not require a semicolon

C++

代码语言:javascript
代码运行次数:0
运行
复制
    class Bar {};

Java

代码语言:javascript
代码运行次数:0
运行
复制
    class Bar {}

Method declarations  方法的声明

Same, except that in Java, must always be part of a class, and may prefix with public/private/protected

Constructors and destructors  构造函数与析构函数

Constructor has same syntax in both (name of the class), Java has no exact equivalent of the destructor

Static member functions and variables 静态函数和变量

Same as method declarations, but Java provides static initialization blocks to initialize static variables (instead of putting a definition in a source code file):

代码语言:javascript
代码运行次数:0
运行
复制
class Foo 
{
    static private int x;
    // static initialization block
    { x = 5; }
}

Scoping static methods and namespaces  静态方法作用域、命名空间

C++

If you have a class and wish to refer to a static method, you use the form Class::method.

代码语言:javascript
代码运行次数:0
运行
复制
class MyClass
{
    public:
    static doStuff();
};

// now it's used like this
MyClass::doStuff();

Java

All scoping in Java uses the . again, just like accessing fields of a class, so it's a bit more regular:

代码语言:javascript
代码运行次数:0
运行
复制
class MyClass
{
    public static doStuff()
    {
        // do stuff
    }
}

// now it's used like this
MyClass.doStuff();

Object declarations  对象声明

C++

代码语言:javascript
代码运行次数:0
运行
复制
    // on the stack
    myClass x;

    // or on the heap
    myClass *x = new myClass;

Java

代码语言:javascript
代码运行次数:0
运行
复制
    // always allocated on the heap (also, always need parens for constructor)
    myClass x = new myClass();

Accessing fields of objects  访问对象域

C++

If you're using a stack-based object, you access its fields with a dot:

代码语言:javascript
代码运行次数:0
运行
复制
myClass x;
x.my_field; // ok

But you use the arrow operator (->) to access fields of a class when working with a pointer:

代码语言:javascript
代码运行次数:0
运行
复制
myClass x = new MyClass;
x->my_field; // ok

Java

You always work with references (which are similar to pointers--see the next section), so you always use a dot:

代码语言:javascript
代码运行次数:0
运行
复制
myClass x = new MyClass();
x.my_field; // ok

References vs. pointers  引用与指针

C++

代码语言:javascript
代码运行次数:0
运行
复制
    // references are immutable, use pointers for more flexibility
    int bar = 7, qux = 6;
    int& foo = bar;

Java

代码语言:javascript
代码运行次数:0
运行
复制
    // references are mutable and store addresses only to objects; there are
    // no raw pointers
    myClass x;
    x.foo(); // error, x is a null ``pointer''

    // note that you always use . to access a field

Inheritance  继承

C++

代码语言:javascript
代码运行次数:0
运行
复制
    class Foo : public Bar
    { ... };

Java

代码语言:javascript
代码运行次数:0
运行
复制
    class Foo extends Bar
    { ... }

Protection levels (abstraction barriers) 保护级别(抽象屏障)

关于抽象的一个形象的隐喻(- - !),把抽象比喻成竖起一道屏障。

C++

代码语言:javascript
代码运行次数:0
运行
复制
    public:
        void foo();
        void bar();

Java

代码语言:javascript
代码运行次数:0
运行
复制
    public void foo();
    public void bar();

Virtual functions 虚函数

C++

代码语言:javascript
代码运行次数:0
运行
复制
    virtual int foo(); // or, non-virtually as simply int foo();

Java

代码语言:javascript
代码运行次数:0
运行
复制
    // functions are virtual by default; use final to prevent overriding
    int foo(); // or, final int foo();

Abstract classes 抽象类

C++

代码语言:javascript
代码运行次数:0
运行
复制
    // just need to include a pure virtual function
    class Bar { public: virtual void foo() = 0; };

Java

代码语言:javascript
代码运行次数:0
运行
复制
    // syntax allows you to be explicit!
    abstract class Bar { public abstract void foo(); }

    // or you might even want to specify an interface
    interface Bar { public void foo(); }

    // and later, have a class implement the interface:
    class Chocolate implements Bar
    {
        public void foo() { /* do something */ }
    }

Memory management 内存管理

Roughly the same--new allocates, but no delete in Java since it has garbage collection.

NULL vs. null

C++

代码语言:javascript
代码运行次数:0
运行
复制
    // initialize pointer to NULL
    int *x = NULL;

Java

代码语言:javascript
代码运行次数:0
运行
复制
    // the compiler will catch the use of uninitialized references, but if you
    // need to initialize a reference so it's known to be invalid, assign null
    myClass x = null;

Booleans 布尔值

Java is a bit more verbose(冗长的): you must write boolean instead of merely bool.

C++

代码语言:javascript
代码运行次数:0
运行
复制
bool foo;

Java

代码语言:javascript
代码运行次数:0
运行
复制
boolean foo;

Const-ness(常量性)

C++

代码语言:javascript
代码运行次数:0
运行
复制
    const int x = 7;

Java

代码语言:javascript
代码运行次数:0
运行
复制
    final int x = 7;

Throw Spec 异常检测

First, Java enforce throw specs at compile time--you must document if your method can throw an exception

C++

代码语言:javascript
代码运行次数:0
运行
复制
int foo() throw (IOException)

Java

代码语言:javascript
代码运行次数:0
运行
复制
int foo() throws IOException

Arrays 数组

C++

代码语言:javascript
代码运行次数:0
运行
复制
    int x[10];
    // or 
    int *x = new x[10];
    // use x, then reclaim memory
    delete[] x;

Java

代码语言:javascript
代码运行次数:0
运行
复制
    int[] x = new int[10];
    // use x, memory reclaimed by the garbage collector or returned to the
    // system at the end of the program's lifetime

Collections and Iteration 集合类与迭代

C++

Iterators are members of classes. The start of a range is <container>.begin(), and the end is <container>.end(). Advance using ++ operator, and access using *.

代码语言:javascript
代码运行次数:0
运行
复制
    vector myVec;
    for ( vector<int>::iterator itr = myVec.begin();
          itr != myVec.end();
          ++itr )
    {
        cout << *itr;
    }

Java

Iterator is just an interface. The start of the range is <collection>.iterator, and you check to see if you're at the end with itr.hasNext(). You get the next element using itr.next() (a combination of using ++ and * in C++).

代码语言:javascript
代码运行次数:0
运行
复制
    ArrayList myArrayList = new ArrayList();
    Iterator itr = myArrayList.iterator();
    while ( itr.hasNext() )
    {
        System.out.println( itr.next() );
    }

    // or, in Java 5
    ArrayList myArrayList = new ArrayList();
    for( Object o : myArrayList ) {
        System.out.println( o );
    }

By Alex Allain

源地址: http://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年08月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C++
  • Java
  • C++
  • Java
  • Class declarations 类的声明
    • C++
    • Java
  • Method declarations  方法的声明
  • Constructors and destructors  构造函数与析构函数
  • Static member functions and variables 静态函数和变量
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
  • NULL vs. null
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
    • C++
    • Java
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档