转:C++和Java的语法对比
2009-11-25 10:44
405 查看
因为要同时用到C++和Java,很多东西容易搞混,这里转了一个比较好的贴子:C++和Java的语法对比
地址:http://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html
以下是英文原帖,还有一个翻译过来的帖子供参考:http://www.cppprog.com/2009/0424/107.html
lots of little differences.
public/private/protected
equivalent of the destructor
blocks
to initialize static variables (instead of putting a definition in
a source code file):
allocates, but no delete
in Java since it
has garbage collection.
method can throw an exception
<container>.begin(), and the end is <container>.end().
Advance using ++ operator, and access using *.
<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++).
for a good introduction.
地址:http://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html
以下是英文原帖,还有一个翻译过来的帖子供参考:http://www.cppprog.com/2009/0424/107.html
C++ and Java Syntax Differences Cheat Sheet
First, two big things--the main function and how to compile it, followed bylots of little differences.
main function
C++
// free-floating function int main( int argc, char* argv[]) { printf( "Hello, world" ); }
Java
// 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++
// compile as g++ foo.cc -o outfile // run with ./outfile
Java
// 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 semicolonC++
class Bar {};
Java
class Bar {}
Method declarations
Same, except that in Java, must always be part of a class, and may prefix withpublic/private/protected
Constructors and destructors
Constructor has same syntax in both (name of the class), Java has no exactequivalent of the destructor
Static member functions and variables
Same as method declarations, but Java provides static initializationblocks
to initialize static variables (instead of putting a definition in
a source code file):
class Foo { static private int x; // static initialization block { x = 5; } }
Object declarations
C++
// on the stack myClass x; // or on the heap myClass *x = new myClass;
Java
// always allocated on the heap (also, always need parens for constructor) myClass x = new myClass();
References vs. pointers
C++
// references are immutable, use pointers for more flexibility int bar = 7, qux = 6; int& foo = bar;
Java
// references are mutuable 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++
class Foo : public Bar { ... };
Java
class Foo extends Bar { ... }
Protection levels (abstraction barriers)
C++
public: void foo(); void bar();
Java
public void foo(); public void bar();
Virtual functions
C++
virtual int foo(); // or, non-virtually as simply int foo()
Java
// functions are virtual by default; use final to prevent overriding int foo(); // or, final int foo();
Abstract classes
C++
// just need to include a pure virtual function class Bar { public: virtual void foo() = 0; };
Java
// 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--newallocates, but no delete
in Java since it
has garbage collection.
NULL vs. null
C++
// initialize pointer to NULL int *x = NULL;
Java
// 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++
bool foo;
Java
boolean foo;
Const-ness
C++
const int x = 7;
Java
final int x = 7;
Throw Spec
First, Java enforce throw specs at compile time--you must document if yourmethod can throw an exception
C++
int foo() throw (IOException)
Java
int foo() throws IOException
Arrays
C++
int x[10]; // or int *x = new x[10]; // use x, then reclaim memory delete[] x;
Java
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 *.
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++).
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 ); }
Templates
This is still being to be added. See http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdffor a good introduction.
相关文章推荐
- Java C++语法对比
- C++ Java C#.net VB.net 语法对比(转)
- java与c++的语法对比
- c++语法学习(与java、python语法对比)
- C、C++、Java三种语言语法对比(一)
- C++和Java的语法对比手册
- Java&C++的基本语法对比
- C、C++、Python、JavaScript、PHP、Java语法对比(纠正篇)
- Java & C++ 数组声明和使用语法对比
- c#语法与c++ 及 java语法的对比分析
- 【转载】C++与Java的语法区别
- java与C++语法上的差别
- OC ,C++,JAVA,SWIFT语法对比
- paip. erlang语法C++语法对比attilax总结
- JAVA、C、C++基本语法的比较
- 对比JAVA和C++
- 从Android到iOS开发——(1)、objective-c与java语法对比
- 慎用++运算符,java和C++中的一个对比
- JAVA – 虚函数、抽象函数、抽象类、接口_对比C++
- VB.NET 、Java 与 C# 语法对比。